Android组件 图片的缩放以旋转(Matrix)

/res/layout/main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="#ff929854">
    
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        
        <Button 
            android:id="@+id/rotateLeft"
            android:text="左旋"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button 
            android:id="@+id/rotateRight"
            android:text="右旋"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Button 
            android:id="@+id/scaleBig"
            android:text="放大"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        
        <Button 
            android:id="@+id/scaleSmall"
            android:text="缩小"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>
    
    <ImageView 
        android:id="@+id/pic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/p2"
        android:layout_gravity="center"/>

</LinearLayout>

Java代码如下:

package com.demo.android.matrix;

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;

public class MatrixActivity extends Activity {
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        addFunctions();
    }
    
    private void addFunctions() {
    	//左旋
    	rotateLeft.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//逆时针旋转90度
				myMatrix.postRotate(-90);
				//创建新的图片,重新绘图
				Bitmap bitmap=Bitmap.createBitmap(myBitmap,0,0,width,height,myMatrix,true);
				//将Bitmap对象转换为Drawable,使其可以用在ImageView和ImageButton中
				BitmapDrawable newbmp=new BitmapDrawable(bitmap);
				pic.setImageDrawable(newbmp);
			}
		});
    	
    	//右旋
    	rotateRight.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				myMatrix.postRotate(90);
				Bitmap bitmap=Bitmap.createBitmap(myBitmap, 0, 0, width, height, myMatrix, true);
				BitmapDrawable newDrawable=new BitmapDrawable(bitmap);
				pic.setImageDrawable(newDrawable);
			}
		});
    	
    	//放大
    	scaleBig.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				myMatrix.postScale(1.2f, 1.2f);
				Bitmap newBitmap=Bitmap.createBitmap(myBitmap, 0, 0, width, height,myMatrix, true);
				BitmapDrawable newDrawable=new BitmapDrawable(newBitmap);
				pic.setImageDrawable(newDrawable);
			}
		});
    	
    	//缩小
    	scaleSmall.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				myMatrix.postScale(0.8f, 0.8f);
				Bitmap newBitmap=Bitmap.createBitmap(myBitmap, 0, 0, width, height,myMatrix, true);
				BitmapDrawable newDrawable=new BitmapDrawable(newBitmap);
				pic.setImageDrawable(newDrawable);
			}
		});
	}
    
    private Bitmap myBitmap;
    private Matrix myMatrix=new Matrix();
    private int width;
    private int height;
	private Button rotateLeft;
    private Button rotateRight;
    private Button scaleBig;
    private Button scaleSmall;
    private ImageView pic;

	private void findViews() {
		rotateLeft=(Button) findViewById(R.id.rotateLeft);
		rotateRight=(Button) findViewById(R.id.rotateRight);
		scaleBig=(Button) findViewById(R.id.scaleBig);
		scaleSmall=(Button) findViewById(R.id.scaleSmall);
		pic=(ImageView) findViewById(R.id.pic);
		
		//获取资源文件的Bitmap
		//getResources()获取系统资源文件下的资源
		myBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.p2);
		width=myBitmap.getWidth();
		height=myBitmap.getHeight();
	}
	
	
}

源代码下载
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值