以编程方式减少ImageView的大小(使用java代码)

问题描述:

如何将imageview的大小减小到200x200?以编程方式减少ImageView的大小(使用java代码)

这是我的实际代码:

private ImageView splash; 

    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     FrameLayout fl = new FrameLayout(this.getApplicationContext()); 
     splash = new ImageView(getApplicationContext()); 
     splash.setImageResource(R.drawable.logo); 
     fl.addView(splash); 
     fl.setForegroundGravity(Gravity.CENTER); 
     fl.setBackgroundColor(Color.BLACK); 
     setContentView(fl); 

就试试这个:

ImageView image=(ImageView)findViewById(R.id.Image1); 
    Bitmap bm=BitmapFactory.decodeResource(getResources(), R.drawable.sc01); 
    int width=200; 
    int height=200; 
    Bitmap resizedbitmap=Bitmap.createScaledBitmap(bm, width, height, true); 

    image.setImageBitmap(resizedbitmap); 

使用此代码,如果你的意思是200浸添加视图的FrameLayout

splash.setLayoutParams(new FrameLayout.LayoutParams(200,200)); 

之前,以下应该工作。

int dips = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, 
    200 , 
    context.getResources().getDisplayMetrics()); 

splash.setLayoutParams(new FrameLayout.LayoutParams(dips,dips)); 

,如果你想获得SP(缩放的像素),而不是用它代替COMPLEX_UNIT_DIP TypedValue.COMPLEX_UNIT_SP,如果你只想平淡200像素,使用

splash.setLayoutParams(new FrameLayout.LayoutParams(200, 200)); 

代替。

可以设置ImageView的大小像这样

imageview.setLayoutParams(new FrameLayout.LayoutParams(200,200)); 
imageview.setScaleType(ScaleType.FIT_XY); 
imageview.setImageResource(R.drawable.logo);