开发者

I have an image which i would like to have grow everytime i click a button

开发者 https://www.devze.com 2023-04-11 22:34 出处:网络
Is 开发者_开发技巧there anyway i could scale an image to become 1.25x bigger everytime a button is pushed?

Is 开发者_开发技巧there anyway i could scale an image to become 1.25x bigger everytime a button is pushed? Basically, could someone give me some tips on how to do this? An explanation, a link, a tutorial, anything would be helpful :) thanks in advance


You should follow this.

http://www.anddev.org/resize_and_rotate_image_-_example-t621.html

Here is code from this page. It creates a Matrix to perform operations(resize and rotate) and applies that matrix to create new BitMap.

You can add the code(with some modifications) on OnClickListener event of yout Button.

public class bitmaptest extends Activity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        LinearLayout linLayout = new LinearLayout(this);

        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
               R.drawable.android);

        int width = bitmapOrg.width();
        int height = bitmapOrg.height();
        int newWidth = 200;
        int newHeight = 200;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(45);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
                          width, height, matrix, true);

        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        ImageView imageView = new ImageView(this);

        // set the Drawable on the ImageView
        imageView.setImageDrawable(bmd);

        // center the Image
        imageView.setScaleType(ScaleType.CENTER);

        // add ImageView to the Layout
        linLayout.addView(imageView,
                new LinearLayout.LayoutParams(
                      LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
                )
        );

        // set LinearLayout as ContentView
        setContentView(linLayout);
    }
}


You can use Bitmap.createScaledBitmap() to resize image. Refer to my articles on Image Processing to get some idea :): https://xjaphx.wordpress.com/learning/tutorials/

0

精彩评论

暂无评论...
验证码 换一张
取 消