NullPointerException
NullPointerException

Reputation: 37579

Reducing the size of a ImageView programatically (with java code)

How to reduce the size of the imageview to 200x200 ?

This is my actual code:

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

Upvotes: 1

Views: 5355

Answers (4)

MKJParekh
MKJParekh

Reputation: 34291

you can set imageview size like this

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

Upvotes: 1

P.Melch
P.Melch

Reputation: 8180

If you mean 200 dip, the following should work.

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

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

if you want to get sp ( scaled pixels ) instead use TypedValue.COMPLEX_UNIT_SP instead of COMPLEX_UNIT_DIP, and if you want just 200 plain pixels, use

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

instead.

Upvotes: 1

PH7
PH7

Reputation: 3916

use this code before you add view to framelayout

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

Upvotes: 2

Uttam
Uttam

Reputation: 12399

Just Try This:

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

Upvotes: 7

Related Questions