作者:陈凤湖南 | 来源:互联网 | 2023-09-09 12:24
问题描述自定义viewpublicclassPasterViewextendsView{privatefinalstaticintMODE_NONE0;pri
====================问题描述====================
自定义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())
<
0.1
&
&
Math.abs(firstY
-
event.getY())
<
0.1){
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;
}
}
布局
<
?xml
version="1.0"
encoding="utf-8"?>
<
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
>
<
RelativeLayout
android:id="@+id/rlTop"
android:layout_
android:layout_
android:background="@color/xx_translucence_camera"
>
<
ImageView
android:id="@+id/ivCancel"
android:layout_
android:layout_
android:src="@drawable/xx_cancel_shot"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
/>
<
ImageView
android:id="@+id/ivAddPaster"
android:layout_
android:layout_
android:src="@drawable/xxx_add_paster"
android:layout_marginRight="20dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:scaleType="center"
/>
<
/RelativeLayout>
<
ImageView
android:id="@+id/ivPhoto"
android:layout_below="@id/rlTop"
android:layout_
android:layout_
android:scaleType="fitXY"
/>
<
com.xx.xxx.xxxxx.PasterView
android:id="@+id/pasterView"
android:layout_below="@id/rlTop"
android:layout_
android:layout_
android:clickable="true"
android:background="#00000000"
android:visibility="gone"/>
<
RelativeLayout
android:id="@+id/rlMid"
android:layout_below="@id/ivPhoto"
android:layout_above="@+id/rlBottom"
android:layout_
android:layout_
android:background="@color/xx_translucence_camera"
>
<
HorizontalScrollView
android:layout_
android:layout_
android:scrollbars="none"
android:layout_alignParentBottom="true"
>
<
LinearLayout
android:id="@+id/llLens"
android:layout_
android:layout_
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
>
<
/LinearLayout>
<
/HorizontalScrollView>
<
/RelativeLayout>
<
RelativeLayout
android:id="@+id/rlBottom"
android:layout_
android:layout_
android:background="@color/xxx_black2"
android:layout_alignParentBottom="true"
>
<
TextView
android:id="@+id/tvShotAgain"
android:layout_
android:layout_
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"
/>
<
ImageView
android:id="@+id/ivOk"
android:layout_centerInParent="true"
android:layout_
android:layout_
android:background="@drawable/xxx_btn_bg"
android:src="@drawable/done"
android:scaleType="center"
/>
<
/RelativeLayout>
<
/RelativeLayout>
====================解决方案1====================
public
Matrix
getMatrix()
=>
public
Matrix
mgetMatrix()自定义View在android2.3.3手机上正常运行 在android4.2.2手机上报空指针