Android圖片左右移動

來源:互聯網
上載者:User

標籤:android圖片左右移動

這個博文,是在上一篇的基礎上修改而成,這次是控製圖片左右移動,上下移動類似就不在寫了

    

package com.yuanlp.testimage;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.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    private ImageView iv;    private int dx=0;  //左移的距離    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        iv = (ImageView) findViewById(R.id.iv);    }    public void turnBig(View view){        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);        Matrix matrix=new Matrix();        matrix.setScale(2,2);  //放大四倍,水平方向2倍,豎直方向2倍        //買一張紙        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth() * 2, bitmap.getHeight() * 2, bitmap.getConfig());        //買個畫板        Canvas canvas = new Canvas(bitmap1);        //臨摹會話        Paint paint = new Paint();        paint.setColor(Color.BLACK);        canvas.drawBitmap(bitmap,matrix,paint);        //傳入新紙        iv.setImageBitmap(bitmap1);    }    public void turnSmall(View view){        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);        Matrix matrix=new Matrix();        matrix.setScale(0.5f,0.5f);        //買個紙        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getConfig());        //買個畫板        Canvas canvas=new Canvas(bitmap1);        Paint paint=new Paint();        paint.setColor(Color.BLACK);        canvas.drawBitmap(bitmap,matrix,paint);        iv.setImageBitmap(bitmap1);    }    public void turnLeft(View view){        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);        dx--;  //每次點擊,向左移動        Matrix matrix=new Matrix();        matrix.setTranslate(dx,0);  //豎直方向不移動,只有左邊移動        //買個紙        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());        //買個畫板        Canvas canvas=new Canvas(bitmap1);        Paint paint=new Paint();        canvas.drawColor(Color.WHITE);        paint.setColor(Color.BLACK);        canvas.drawBitmap(bitmap,matrix,paint);        iv.setImageBitmap(bitmap1);    }    public void turnRight(View view){        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);        dx++;  //每次點擊,向左移動        Matrix matrix=new Matrix();        matrix.setTranslate(dx,0);  //豎直方向不移動,只有左邊移動        //買個紙        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());        //買個畫板        Canvas canvas=new Canvas(bitmap1);        Paint paint=new Paint();        canvas.drawColor(Color.WHITE);        paint.setColor(Color.BLACK);        canvas.drawBitmap(bitmap,matrix,paint);        iv.setImageBitmap(bitmap1);    }}


本文出自 “YuanGuShi” 部落格,請務必保留此出處http://cm0425.blog.51cto.com/10819451/1943890

Android圖片左右移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.