android自定义view空指针,自定义View在android2.3.3手机上正常运行 在android4.2.2手机上报空指针...

本文介绍了如何在Android中创建一个自定义View,该View允许用户对图片进行拖动、缩放和旋转操作,并提供了一个删除贴纸的功能。通过Matrix矩阵变换实现图像操作,同时在触摸事件中处理不同手势状态,提供了丰富的交互体验。
摘要由CSDN通过智能技术生成

============问题描述============

自定义view

public class PasterView extends View

{

private final static int MODE_NONE = 0;

private final static int MODE_DRAG = 1;

private final static int MODE_ZOOM = 2;

private int mode = MODE_NONE;

private Paint paint;

private Matrix matrix;

private Bitmap bitmap;

private float oldX;

private float oldY;

private float firstX;

private float firstY;

private float oldDistance;

private PointF midPoint;

private float oldAngle;

private Context context;

private AlertDialog dialog;

public PasterView(Context context)

{

super(context);

this.context = context;

paint = new Paint();

paint.setAntiAlias(true);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.menu_camera);

}

public PasterView(Context context, AttributeSet attrs)

{

super(context, attrs);

this.context = context;

paint = new Paint();

paint.setAntiAlias(true);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.menu_camera);

}

public PasterView(Context context, AttributeSet attrs, int defStyle)

{

super(context, attrs, defStyle);

this.context = context;

paint = new Paint();

paint.setAntiAlias(true);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.menu_camera);

}

@Override

protected void onDraw(Canvas canvas)

{

if(bitmap != null){

if(matrix == null){

matrix = new Matrix();

matrix.postTranslate((getWidth() - bitmap.getWidth())/2, (getHeight() - bitmap.getHeight())/2);

midPoint = new PointF(getWidth()/2, getHeight()/2);

}

canvas.drawBitmap(bitmap, matrix, paint);

}

super.onDraw(canvas);

}

@Override

public boolean onTouchEvent(MotionEvent event)

{

switch(event.getAction() & MotionEvent.ACTION_MASK){

case MotionEvent.ACTION_DOWN:

mode = MODE_DRAG;

oldX = event.getX();

oldY = event.getY();

firstX = event.getX();

firstY = event.getY();

break;

case MotionEvent.ACTION_POINTER_DOWN:

mode = MODE_ZOOM;

oldDistance = getDistance(event);

//midPoint = getMidPoint(event);

oldAngle = getAngle(event);

break;

case MotionEvent.ACTION_MOVE:

if(mode == MODE_DRAG){

float x = event.getX();

float y = event.getY();

matrix.postTranslate(x - oldX, y - oldY);

midPoint.x += (x - oldX);

midPoint.y += (y - oldY);

invalidate();

oldX = x;

oldY = y;

}

else if(mode == MODE_ZOOM){

float fDistance = getDistance(event);

float scale = fDistance/oldDistance;

matrix.postScale(scale, scale, midPoint.x, midPoint.y);

float angle = getAngle(event);

matrix.postRotate((float) ((oldAngle - angle)*180/3.1415926), midPoint.x, midPoint.y);

invalidate();

oldDistance = fDistance;

oldAngle = angle;

}

break;

case MotionEvent.ACTION_UP:

if(mode == MODE_DRAG){

if(Math.abs(firstX - event.getX()) 

if(dialog == null){

AlertDialog.Builder builder = new AlertDialog.Builder(context);

builder.setMessage("是否删除贴纸?");

builder.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener()

{

@Override

public void onClick(DialogInterface dialog, int which)

{

dialog.cancel();

}

});

builder.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener()

{

@Override

public void onClick(DialogInterface dialog, int which)

{

dialog.dismiss();

reset();

setVisibility(View.GONE);

}

});

dialog = builder.create();

}

dialog.show();

}

}

mode = MODE_NONE;

break;

case MotionEvent.ACTION_POINTER_UP:

mode = MODE_NONE;

break;

}

return super.onTouchEvent(event);

}

public Matrix getMatrix()

{

return matrix;

}

public void setMatrix(Matrix matrix)

{

this.matrix = matrix;

}

public Bitmap getBitmap()

{

return bitmap;

}

public void setBitmap(Bitmap bitmap)

{

this.bitmap = bitmap;

}

private float getDistance(MotionEvent event){

return (float) Math.sqrt(Math.pow(event.getX(0) - event.getX(1), 2) + Math.pow(event.getY(0) - event.getY(1), 2));

}

private PointF getMidPoint(MotionEvent event){

return new PointF((event.getX(0) + event.getX(1))/2, (event.getY(0) + event.getY(1))/2);

}

private float getAngle(MotionEvent event){

return (float) Math.atan2(event.getY(1) - event.getY(0),event.getX(0) - event.getX(1));

}

public void reset(){

matrix = null;

mode = MODE_NONE;

}

public void release(){

if(bitmap != null && !bitmap.isRecycled()){

bitmap.recycle();

bitmap = null;

}

}

布局

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/rlTop"

android:layout_width="match_parent"

android:layout_height="44dp"

android:background="@color/xx_translucence_camera" >

android:id="@+id/ivCancel"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/xx_cancel_shot"

android:padding="10dp"

android:layout_marginLeft="10dp"

android:layout_centerVertical="true" />

android:id="@+id/ivAddPaster"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:src="@drawable/xxx_add_paster"

android:layout_marginRight="20dp"

android:layout_centerVertical="true"

android:layout_alignParentRight="true"

android:scaleType="center" />

android:id="@+id/ivPhoto"

android:layout_below="@id/rlTop"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scaleType="fitXY" />

android:id="@+id/pasterView"

android:layout_below="@id/rlTop"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:clickable="true"

android:background="#00000000"

android:visibility="gone"/>

android:id="@+id/rlMid"

android:layout_below="@id/ivPhoto"

android:layout_above="@+id/rlBottom"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/xx_translucence_camera" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:scrollbars="none"

android:layout_alignParentBottom="true" >

android:id="@+id/llLens"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:focusable="true"

android:focusableInTouchMode="true" >

android:id="@+id/rlBottom"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/xxx_black2"

android:layout_alignParentBottom="true" >

android:id="@+id/tvShotAgain"

android:layout_width="wrap_content"

android:layout_height="match_parent"

android:paddingLeft="10dp"

android:paddingRight="10dp"

android:layout_marginLeft="10dp"

android:text="@string/shot_again"

android:gravity="center"

android:textSize="@dimen/button_textsize"

android:textColor="@color/xxxx_album_btn_text_color" />

android:id="@+id/ivOk"

android:layout_centerInParent="true"

android:layout_width="100dp"

android:layout_height="40dp"

android:background="@drawable/xxx_btn_bg"

android:src="@drawable/done"

android:scaleType="center" />

============解决方案1============

public Matrix getMatrix()

=>

public Matrix mgetMatrix()

原文:http://www.cnblogs.com/llj1985522/p/4041961.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值