关于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>

关于Bitmap和Matrix的用法

时间: 2024-11-03 22:26:51

关于Bitmap和Matrix的用法的相关文章

Android Bitmap 用法总结

android Bitmap用法总结 Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 1.Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsi

android Bitmap用法总结

Bitmap用法总结1.Drawable → Bitmappublic static Bitmap drawableToBitmap(Drawable drawable) {Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_888

android Bitmap用法总结(转载)

Bitmap用法总结1.Drawable → Bitmappublic static Bitmap drawableToBitmap(Drawable drawable) {Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_888

Android-使用Matrix对Bitmap进行处理

1.Android中使用Matrix对图像进行缩放.旋转.平移.斜切等变换的. Matrix是一个3*3的矩阵,其值对应如下: 下面给出具体坐标对应变形的属性|scaleX, skewX, translateX| |skewY, scaleY, translateY||0       ,0        , scale       | Matrix提供了一些方法来控制图片变换:setTranslate(float dx,float dy):控制Matrix进行位移.setSkew(float k

Android图片旋转,缩放,位移,倾斜,对称完整示例(二)——Bitmap.createBitmap()和Matrix

MainActivity如下: package cc.c; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.widget.ImageView; /** * Demo描述: * 利用B

安卓自定义View进阶-Matrix详解

这应该是目前最详细的一篇讲解Matrix的中文文章了,在上一篇文章Matrix原理中,我们对Matrix做了一个简单的了解,偏向理论,在本文中则会详细的讲解Matrix的具体用法,以及与Matrix相关的一些实用技巧. Matrix方法表 按照惯例,先放方法表做概览. 方法类别 相关API 摘要 基本方法 equals hashCode toString toShortString 比较. 获取哈希值. 转换为字符串 数值操作 set reset setValues getValues 设置.

Bitmap具体解释与Bitmap的内存优化

感觉这里的排版看着更舒服些 Bitmap具体解释与Bitmap的内存优化 一.Bitmap: Bitmap是Android系统中的图像处理的最重要类之中的一个.用它能够获取图像文件信息,进行图像剪切.旋转.缩放等操作.并能够指定格式保存图像文件. 常常用法: + public void recycle() // 回收位图占用的内存空间.把位图标记为Dead + public final boolean isRecycled() //推断位图内存是否已释放 + public final int g

Android中图像变换Matrix的原理、代码验证和应用(二)

注:本篇文章为转载文章,因为原文格式排版较乱,但是内容非常棒,所以整理一下,方便以后查看. 查看原文请戳:http://blog.csdn.net/pathuang68/article/details/6991988 Matrix介绍文章请戳:http://blog.csdn.net/pathuang68/article/details/6991867 package com.pat.testtransformmatrix; import android.app.Activity; import

Bitmap工具类

一直在使用的一个Bitmap工具类 处理Bitmap和ImageView对象,实现了以下功能: 1.saveBitmap: 把Bitmap对象持久存储到SD卡或手机内存. 2.getViewBitmap: 从view得到bitmap对象 3.addWatermark: Bitmap加水印 4.zoomBitmap: 放大缩小图片 5.getLoacalBitmap: 传入路径,从持久存储(SD卡或手机内存)得到Bitmap对象 6.getBitMapByUrl: 通过URL地址获取Bitmap对