您的位置:首页 > 其它

自定义View实现图片的绘制、旋转、缩放

2015-04-13 14:40 316 查看
1、图片

把一张JPG图片改名为image.jpg,然后拷贝到项目的res-drawable中。

2、activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button android:id="@+id/buttonLeft"
android:text="图片向左移动"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/buttonRight"
android:text="图片向右移动"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button android:id="@+id/buttonRotationLeft"
android:text="图片向左旋转"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/buttonRotationRight"
android:text="图片向右旋转"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/buttonNarrow"
android:text="图片缩小"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/buttonEnlarge"
android:text="图片放大"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>


 import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Canvas;
import android.widget.LinearLayout;
import android.widget.Button;

public class MainActivity extends Activity {
ImageView imageView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//    动态加载图片到LinearLayout中
imageView = new ImageView(this);
LinearLayout ll = (LinearLayout) findViewById(R.id.imageid);
ll.addView(imageView);
//    向左移动
Button btnLeft = (Button) findViewById(R.id.buttonLeft);
btnLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setPosLeft();
}
});
//    向右移动
Button btnRight = (Button) findViewById(R.id.buttonRight);
btnRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setPosRight();
}
});

//    向左旋转
Button btnRotationLeft = (Button)findViewById(R.id.buttonRotationLeft);
btnRotationLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setRotationLeft();
}
});
//    向右旋转
Button btnRotationRight = (Button)findViewById(R.id.buttonRotationRight);
btnRotationRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setRotationRight();
}
});

//    放大图片
Button btnEnlarge = (Button)findViewById(R.id.buttonEnlarge);
btnEnlarge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setEnlarge();
}
});
//    缩小图片
Button btnNarrow = (Button)findViewById(R.id.buttonNarrow);
btnNarrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setNarrow();
}
});
}

//    自定义图片View
class ImageView extends View {
private Paint paint = null;    //    画笔
private Bitmap bitmap = null;    //    图片位图
private Bitmap bitmapDisplay = null;
private Matrix matrix = null;
private int nBitmapWidth = 0;    //    图片的宽度
private int nBitmapHeight = 0;    //    图片的高度
private int nPosX = 120;    //    图片所在的位置X
private int nPosY = 10;    //    图片所在的位置Y
private float fAngle = 0.0f;    //    图片旋转
private float fScale = 1.0f;    //    图片缩放 1.0表示为原图

public ImageView(Context context) {
super(context);

paint = new Paint();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);

//    加载需要操作的图片
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
bitmapDisplay = bitmap;

matrix = new Matrix();
//    获取图片高度和宽度
nBitmapWidth = bitmap.getWidth();
nBitmapHeight = bitmap.getHeight();
}

//    向左移动
public void setPosLeft() {
nPosX -= 10;
}
//    向右移动
public void setPosRight() {
nPosX += 10;
}

//    向左旋转
public void setRotationLeft() {
fAngle--;
setAngle();
}
//    向右旋转
public void setRotationRight() {
fAngle++;
setAngle();
}

//    图片放大
public void setEnlarge() {
if (fScale < 2) {
fScale += 0.1f;
setScale();
}
}
//    图片缩小
public void setNarrow() {
if (fScale > 0.5) {
fScale -= 0.1f;
setScale();
}
}

//    设置旋转比例
private void setAngle() {
matrix.reset();
matrix.setRotate(fAngle);
bitmapDisplay = Bitmap.createBitmap(bitmap,0,0,nBitmapWidth,nBitmapHeight,matrix,true);
}

//    设置缩放比例
private void setScale() {
matrix.reset();
matrix.postScale(fScale, fScale);
bitmapDisplay = Bitmap.createBitmap(bitmap,0,0,nBitmapWidth,nBitmapHeight,matrix,true);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmapDisplay, nPosX, nPosY, paint);
invalidate();
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: