Custom View on android2.3.3 mobile phone report null pointer on android4.2.2 phone

Source: Internet
Author: User


==================== Problem Description ====================
Custom 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 ("Do you want to remove the sticker?" ");
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;
}
}


Layout

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<relativelayout
Android:id= "@+id/rltop"
Android:layout_width= "Match_parent"
android:layout_height= "44DP"
android:background= "@color/xx_translucence_camera" >
<imageview
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"/>
<imageview
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"/>
</RelativeLayout>
<imageview
Android:id= "@+id/ivphoto"
android:layout_below= "@id/rltop"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:scaletype= "Fitxy"/>
<com.xx.xxx.xxxxx.pasterview
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"/>
<relativelayout
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" >
<linearlayout
Android:id= "@+id/lllens"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:orientation= "Horizontal"
Android:focusable= "true"
Android:focusableintouchmode= "true" ></LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<relativelayout
Android:id= "@+id/rlbottom"
Android:layout_width= "Match_parent"
android:layout_height= "50DP"
android:background= "@color/xxx_black2"
Android:layout_alignparentbottom= "true" >
<textview
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"/>
<imageview
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"/>
</RelativeLayout>
</RelativeLayout>
==================== Solution 1====================
Public Matrix Getmatrix ()
=
Public Matrix Mgetmatrix ()

Custom View on android2.3.3 mobile phone report null pointer on android4.2.2 phone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.