zoukankan      html  css  js  c++  java
  • Android之图片应用

    package com.example.imagescale;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
        private ImageView iv1,iv2;
        private Button bt,bt2,bt3,bt4;
        private Bitmap bp;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv1=(ImageView)this.findViewById(R.id.imageView1);
            iv2=(ImageView)this.findViewById(R.id.imageView2);
            bt=(Button)this.findViewById(R.id.button1);
            bt2=(Button)this.findViewById(R.id.button2);
            bt3=(Button)this.findViewById(R.id.button3);
            bt4=(Button)this.findViewById(R.id.button4);
            bt.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);
                           //iv1.setImageBitmap(bp);
                           //对照图片
                           //以bp在高、宽决定Canvas画布的大小
                           Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//
                           Canvas canvas = new Canvas(bp2);
                           Paint paint = new Paint();
                           paint.setColor(Color.BLACK);
                           Matrix matrix = new Matrix();
                        matrix.setScale(0.5f, 1);
                        canvas.drawBitmap(bp, matrix, paint);
                       iv2.setImageBitmap(bp2);
                    }
                });
            bt2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);
                       //iv1.setImageBitmap(bp);
                       //对照图片
                       //以bp在高、宽决定Canvas画布的大小
                       Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//
                       Canvas canvas = new Canvas(bp2);
                       Paint paint = new Paint();
                       paint.setColor(Color.BLACK);
                       Matrix matrix = new Matrix();
                       matrix.setScale(1, -1);
                       matrix.postTranslate(0, bp.getHeight());
                    canvas.drawBitmap(bp, matrix, paint);
                   iv2.setImageBitmap(bp2);
                }
            });
            bt3.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);
                       //iv1.setImageBitmap(bp);
                       //对照图片
                       //以bp在高、宽决定Canvas画布的大小
                       Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//
                       Canvas canvas = new Canvas(bp2);
                       Paint paint = new Paint();
                       paint.setColor(Color.BLACK);
                       Matrix matrix = new Matrix();
                       matrix.setRotate(90, bp.getWidth()/2, bp.getHeight()/2);
                       canvas.drawBitmap(bp, matrix, paint);
                   iv2.setImageBitmap(bp2);
                }
            });
            bt4.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);
                       //iv1.setImageBitmap(bp);
                       //对照图片
                       //以bp在高、宽决定Canvas画布的大小
                       Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//
                       Canvas canvas = new Canvas(bp2);
                       Paint paint = new Paint();
                       paint.setColor(Color.BLACK);
                       Matrix matrix = new Matrix();
                       matrix.setTranslate(100, 100);
                    canvas.drawBitmap(bp, matrix, paint);
                   iv2.setImageBitmap(bp2);
                }
            });
                    //原始图片
                    //bp = BitmapFactory.decodeResource(getResources(), R.drawable.xh);
                       //iv1.setImageBitmap(bp);
                       //对照图片
                       //以bp在高、宽决定Canvas画布的大小
                       //Bitmap bp2 = Bitmap.createBitmap(bp.getWidth(), bp.getHeight(), bp.getConfig());//
                       //Canvas canvas = new Canvas(bp2);
                       //Paint paint = new Paint();
                       //paint.setColor(Color.BLACK);
                       //Matrix matrix = new Matrix();
                       //1.缩放
                           //方法一:使用矩阵
                           /*matrix.setValues(new float[]{
                                   0.5f,0,0,
                                   0,1,0,
                                   0,0,1
                           });*/
                           //方法二:
                           //matrix.setScale(0.5f, 1);
                       //2.旋转
                           //以图片左上角为中心旋转
                           //matrix.setRotate(15);
                           //以图片中心旋转
                           //matrix.setRotate(90, bp.getWidth()/2, bp.getHeight()/2);
                           //消除锯齿
                           //paint.setAntiAlias(true);
                       //3.平移
                           //matrix.setTranslate(50, 50);
                       //4.镜子效果
                           //matrix.setScale(-1, 1);
                           //matrix.postTranslate(bp.getWidth(), 0);
                        //5.倒影效果
                           //matrix.setScale(1, -1);
                           //matrix.postTranslate(0, bp.getHeight());
                           
                           //canvas.drawBitmap(bp, matrix, paint);
                           //iv2.setImageBitmap(bp2);
           
        }
    
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
        
    }

    执行效果图:

    不努力,还要青春干什么?
  • 相关阅读:
    IOS应用开发版本控制工具之Versions使用,iosversions
    关于cocoapods安装与使用的总结
    Objective-c单例模式的正确写法--用dispatch 线程安全
    C语言中Static和Const关键字的的作用 -- 转
    OC中protocol、category和继承的关系--转
    iOS- NSThread/NSOperation/GCD 三种多线程技术的对比及实现 -- 转
    Object C学习笔记18-SEL,@ selector,Class,@class--转
    COPY, RETAIN, ASSIGN , READONLY , READWRITE,STRONG,WEAK,NONATOMIC整理--转
    Ubuntu下环境变量该写进哪个文件里
    python 写一个贪吃蛇游戏
  • 原文地址:https://www.cnblogs.com/caidupingblogs/p/5051433.html
Copyright © 2011-2022 走看看