Android 구성 요소 이미지 크기 조절(Matrix)

5704 단어
/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();
	}
	
	
}

소스 코드 다운로드

좋은 웹페이지 즐겨찾기