上按一下按钮旋转图像 [英] Rotating image on button click

查看:128
本文介绍了上按一下按钮旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了一个小的应用程序,这样当我点击按钮,显示有图像旋转应。我写了下面的code:

I had created a small app such that the image which is displaying should rotate when I click the button. I have written the following code:

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.widget.ImageView;


public class ImageActivity extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }  
    public void Rotate()
    {

        ImageView img = (ImageView)findViewById(R.id.imageView01);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.bharath);
// Getting width & height of the given image.
        int w = bmp.getWidth();
        int h = bmp.getHeight();
// Setting pre rotate to 90
        Matrix mtx = new Matrix();
        mtx.preRotate(90);
// Rotating Bitmap
        Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, w, h, mtx, true);
        BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
        img.setImageDrawable(bmd);
    }

现在,当我执行code,然后单击按钮旋转图像,应用程序迫使我关闭它,提有一个意外的错误。我无法追踪和正确的。任何一个可以请帮我在这方面?

Now when I execute the code and click the button to rotate the image, the application is forcing me to close it, mentioning there is an unexpected error. I could not trace and correct. Can any one please help me in this regard?

推荐答案

还是让我猜,你有布局的main.xml code>,有此属性安卓的onClick =旋转,其中在 ImageActivity ,不是吗?

Lemme guess, you have Button in the layout main.xml, having this attribute android:onClick="Rotate", in which the method defined in the ImageActivity, don't you?

如果这是真的,重写的定义旋转()是这样的:公共无效旋转(视图V)
它应该是工作的罚款。

If it is true, rewrite the definition of Rotate() like this: public void Rotate(View v). It should be working fine.

此外,你可以使用:

img.setImageBitmap(rotatedBMP);

,而不是上浪费更多的内存 BitmapDrawable

这篇关于上按一下按钮旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆