android自定义view拉扯图片,android自定义View实现图片的绘制、旋转、缩放

1、图片

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

2、activity_main.xml

android:id="@+id/imageid"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:text="图片向左移动"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:text="图片向右移动"

android:layout_width="fill_parent"

android:layout_height="wrap_content"/>

android:text="图片向左旋转"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:text="图片向右旋转"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:text="图片缩小"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

android:text="图片放大"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

3、MainActivity.java类

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();

}

}

}

4、效果图

1b3979f19185034eb09ee5eacd03f8b5.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值