zoukankan      html  css  js  c++  java
  • 关于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 + "
    " + 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>
  • 相关阅读:
    运行ConnectionDemo时遇到的问题及解决方案
    xampp启动MySQL出现Error: MySQL shutdown unexpectedly.
    20175227张雪莹 2018-2019-2 《Java程序设计》第八周学习总结
    KMS
    MAC 添加共享,脚本执行
    zabbix企业应用之windows系统安装omsa硬件监控
    SCCM大致安装过程,参考前辈教程完成部署
    MAC加域重复跳出---"talagent"想使用“本地项目” 的钥匙串
    CentOS Linux解决 Device eth0 does not seem to be present
    zabbix3.0.4 部署History
  • 原文地址:https://www.cnblogs.com/mf0819/p/3875892.html
Copyright © 2011-2022 走看看