2011-10-13 74 views
1

如何將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); 

回答

7

就試試這個:

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); 
2

使用此代碼,如果你的意思是200浸添加視圖的FrameLayout

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

之前,以下應該工作。

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)); 

代替。

1

可以設置ImageView的大小像這樣

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