通过设置Bitmap旋转ImageView(动态旋转图片)

java:

package EX04_25.txt;

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.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

public class EX04_25 extends Activity {
	private Button LButton;
	private Button RButton;
	private ImageView mImageView;
	
	private int degrean = 0;	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initApp();
    }

	private void initApp() {
		LButton = (Button)findViewById(R.id.leftButton);
		RButton = (Button)findViewById(R.id.rightButton);
		mImageView = (ImageView)findViewById(R.id.myImage);
		final Bitmap bmp= BitmapFactory.decodeResource
		(getResources(), R.drawable.woman);
		final int mWidth = bmp.getWidth();
		final int mHeight = bmp.getHeight();

		
		mImageView.setScaleType(ScaleType.CENTER); //这项设置为正常旋转Bitmap的关键
		// TODO Auto-generated method stub
		LButton.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				turnLeft();
			}

			private void turnLeft() {
				// TODO Auto-generated method stub
				degrean--;
				if (degrean < -5) {
					degrean = -5;
				}
				Matrix mt = new Matrix();
				mt.postRotate(5*degrean);
				Bitmap turnBmp = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight, mt, true);
				mImageView.setImageBitmap(turnBmp);
			}			
		});
		
		RButton.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				turnRight();
			}

			private void turnRight() {
				// TODO Auto-generated method stub
				degrean++;
				if (degrean > 5) {
					degrean = 5;
				}
				Matrix mt = new Matrix();
				mt.postScale(1, 1);
				mt.setRotate(5*degrean);
				Bitmap turnBmp = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight, mt, true);
				BitmapDrawable turnDrawable = new BitmapDrawable(turnBmp);
				
				mImageView.setImageDrawable(turnDrawable);
			}			
		});
		
	}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Button
	android:id = "@+id/leftButton"
	android:layout_marginTop = "40dip" 
	android:layout_marginLeft = "20dip" 
    android:layout_width="81dip" 
    android:layout_height="wrap_content" 
    android:text="向左旋转"
    />
<ImageView
	android:id = "@+id/myImage"
	android:layout_marginTop = "10dip" 
	android:layout_marginLeft = "10dip" 
    android:layout_width="100dip" 
    android:layout_height="125dip" 
    android:src = "@drawable/woman"
    />    
<Button
	android:id = "@+id/rightButton"
	android:layout_marginTop = "40dip" 
	android:layout_marginLeft = "10dip" 
    android:layout_width="80dip" 
    android:layout_height="wrap_content" 
    android:text="向右旋转"
    />        
</LinearLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值