首页 > 代码库 > 关于Bitmap和Matrix的用法
关于Bitmap和Matrix的用法
通过AsyncTask来加载图片,通过Matrix来实现图片的缩放和旋转
直接看程序
MainAcitivy.java
package com.example.sample_4_22;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.app.ProgressDialog;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.os.AsyncTask;import android.os.Bundle;import android.provider.MediaStore;import android.provider.MediaStore.Images;import android.util.DisplayMetrics;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity { String fpath; List<String> filesPath; ImageView imageView; Button bt; Button bt2; Button bt3; int i = 0; ProgressDialog dialog; float scalex = 1; float scaley = 1; int imageX; int imageY; int widthorig; int heightorig; Bitmap firstmap; Bitmap bitmap; float degrees =10; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); filesPath = new ArrayList<String>(); imageView = (ImageView) findViewById(R.id.imageView1); imageView.setMaxHeight(500); bt = (Button) findViewById(R.id.button1); bt2 = (Button) findViewById(R.id.button2); bt3 = (Button) findViewById(R.id.button3); dialog = new ProgressDialog(this); dialog.setMessage("正在加载中..."); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); imageX = dm.widthPixels; imageY = dm.heightPixels - 200; Cursor cursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, Images.Media.BUCKET_DISPLAY_NAME); if (cursor != null) { cursor.moveToFirst(); while (cursor.moveToNext()) { fpath = cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); filesPath.add(fpath); } } firstmap = BitmapFactory.decodeFile(filesPath.get(0)); imageView.setImageBitmap(firstmap); widthorig = firstmap.getWidth(); heightorig = firstmap.getWidth(); Log.i("original width and height:", widthorig + "\n" + heightorig); bt.setOnClickListener(new OnClickListener() { @SuppressWarnings("unchecked") public void onClick(View v) { // TODO Auto-generated method stub new MyAsyncTask().execute(filesPath); i++; scalex = 1;// 画图后初始化缩放比例 scaley = 1; degrees=0; } }); bt2.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub new MyScaleAsyncTask().execute(); } }); bt3.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub new MyRotateAsyncTask().execute(firstmap); degrees=degrees+10; } }); } private class MyRotateAsyncTask extends AsyncTask<Bitmap, Void, Bitmap> { @Override protected Bitmap doInBackground(Bitmap... params) { // TODO Auto-generated method stub Matrix matrix = new Matrix(); matrix.postRotate(degrees); Bitmap bitmap = Bitmap.createBitmap(params[0], 0, 0, widthorig, heightorig, matrix, true); return bitmap; } @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub super.onPostExecute(result); imageView.setImageBitmap(result); } } private class MyScaleAsyncTask extends AsyncTask<Bitmap, Void, Bitmap> { @Override protected Bitmap doInBackground(Bitmap... params) { // TODO Auto-generated method stub Matrix matrix = new Matrix(); scalex = (float) (scalex * 0.9); scaley = (float) (scaley * 0.9); matrix.postScale(scalex, scaley); bitmap = Bitmap.createBitmap( BitmapFactory.decodeFile(filesPath.get(i)), 0, 0, 400, 600, matrix, true); return bitmap; } @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub super.onPostExecute(result); imageView.setImageBitmap(bitmap); } } private class MyAsyncTask extends AsyncTask<List<String>, Void, Bitmap> { Bitmap map; @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog.show(); } @Override protected Bitmap doInBackground(List<String>... params) { // TODO Auto-generated method stub String path = params[0].get(i); map = BitmapFactory.decodeFile(path); return map; } @Override protected void onPostExecute(Bitmap result) { // TODO Auto-generated method stub imageView.setImageBitmap(result); firstmap = result; super.onPostExecute(result); dialog.dismiss(); } }}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${packageName}.${activityClass}" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:text="切换图片" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginTop="14dp" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="16dp" android:text="缩小图片" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/imageView1" android:layout_toRightOf="@+id/button1" android:text="旋转图片" /></RelativeLayout>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。