首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

图片的上下移动、旋转及放大缩小

2012-08-03 
图片的左右移动、旋转及放大缩小?xml version1.0 encodingutf-8?LinearLayout xmlns:androidhtt

图片的左右移动、旋转及放大缩小

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/iamgeid"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >        <Button android:id="@+id/buttonLeft"            android:layout_width="fill_parent" android:layout_height="wrap_content"            android:text="图片向左移动"            />        <Button android:id="@+id/buttonRight"            android:layout_width="fill_parent" android:layout_height="wrap_content"            android:text="图片向右移动"            />        <Button android:id="@+id/buttonRotationLeft"            android:layout_width="fill_parent" android:layout_height="wrap_content"            android:text="图片左旋转"            />        <Button android:id="@+id/buttonRotationRight"            android:layout_width="fill_parent" android:layout_height="wrap_content"            android:text="图片右旋转"            />        <Button android:id="@+id/buttonNarrow"            android:layout_width="fill_parent" android:layout_height="wrap_content"            android:text="图片缩小"            />        <Button android:id="@+id/buttonEnlarge"            android:layout_width="fill_parent" android:layout_height="wrap_content"            android:text="图片放大"            /></LinearLayout>


package cn.m15.xys;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.LinearLayout;public class Image extends Activity {    ImageView imageView = null;    @Override    protected void onCreate(Bundle savedInstanceState) {imageView = new ImageView(this);setContentView(R.layout.image);LinearLayout ll = (LinearLayout) findViewById(R.id.iamgeid);ll.addView(imageView);// 向左移动Button botton0 = (Button) findViewById(R.id.buttonLeft);botton0.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View arg0) {imageView.setPosLeft();    }});// 向右移动Button botton1 = (Button) findViewById(R.id.buttonRight);botton1.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View arg0) {imageView.setPosRight();    }});// 左旋转Button botton2 = (Button) findViewById(R.id.buttonRotationLeft);botton2.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View arg0) {imageView.setRotationLeft();    }});// 右旋转Button botton3 = (Button) findViewById(R.id.buttonRotationRight);botton3.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View arg0) {imageView.setRotationRight();    }});// 缩小Button botton4 = (Button) findViewById(R.id.buttonNarrow);botton4.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View arg0) {imageView.setNarrow();    }});// 放大Button botton5 = (Button) findViewById(R.id.buttonEnlarge);botton5.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View arg0) {imageView.setEnlarge();    }});super.onCreate(savedInstanceState);    }    class ImageView extends View {Paint mPaint = null;Bitmap bitMap = null;Bitmap bitMapDisplay = null;int m_posX = 120;int m_posY = 50;int m_bitMapWidth = 0;int m_bitMapHeight = 0;Matrix mMatrix = null;float mAngle = 0.0f;float mScale = 1f;//1为原图的大小public ImageView(Context context) {    super(context);    mPaint = new Paint();    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);    bitMap = BitmapFactory.decodeResource(this.getResources(),    R.drawable.image);    bitMapDisplay = bitMap;    mMatrix = new Matrix();    // 获取图片宽高    m_bitMapWidth = bitMap.getWidth();    m_bitMapHeight = bitMap.getHeight();}// 向左移动public void setPosLeft() {    m_posX -= 10;}// 向右移动public void setPosRight() {    m_posX += 10;}// 向左旋转public void setRotationLeft() {    mAngle--;    setAngle();}// 向右旋转public void setRotationRight() {    mAngle++;    setAngle();}// 缩小图片public void setNarrow() {    if (mScale > 0.5) {mScale -= 0.1;setScale();    }}// 放大图片public void setEnlarge() {    if (mScale < 2) {mScale += 0.1;setScale();    }}// 设置缩放比例public void setAngle() {    mMatrix.reset();    mMatrix.setRotate(mAngle);    bitMapDisplay = Bitmap.createBitmap(bitMap, 0, 0, m_bitMapWidth,    m_bitMapHeight, mMatrix, true);}// 设置旋转比例public void setScale() {    mMatrix.reset();    //float sx X轴缩放     //float sy Y轴缩放    mMatrix.postScale(mScale, mScale);    bitMapDisplay = Bitmap.createBitmap(bitMap, 0, 0, m_bitMapWidth,    m_bitMapHeight, mMatrix, true);}@Overrideprotected void onDraw(Canvas canvas) {    super.onDraw(canvas);    canvas.drawBitmap(bitMapDisplay, m_posX, m_posY, mPaint);    invalidate();}    }}

热点排行