Android and android

Source: Internet
Author: User

Android and android

Android rewards

Custom View on textView

Import android. content. Context;
Import android. graphics. Bitmap;
Import android. graphics. Bitmap. Config;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Path;
Import android. graphics. PorterDuff. Mode;
Import android. graphics. porterduduxfermode;
Import android. util. AttributeSet;
Import android. view. MotionEvent;
Import android. view. View;

Public class EraseView extends View {

Private boolean isMove = false;
Private Bitmap bitmap = null;
Private Bitmap frontBitmap = null;
Private Path path;
Private Canvas mCanvas;
Private Paint paint;


Public EraseView (Context context, AttributeSet attrs ){
Super (context, attrs );
}
@ Override
Protected void onDraw (Canvas canvas ){

If (mCanvas = null ){
EraseBitmp ();
}
Canvas. drawBitmap (bitmap, 0, 0, null );
MCanvas. drawPath (path, paint );
Super. onDraw (canvas );
}

Public void EraseBitmp (){

Bitmap = Bitmap. createBitmap (getWidth (), getHeight (), Bitmap. Config. ARGB_4444 );

FrontBitmap = CreateBitmap (Color. GRAY, getWidth (), getHeight ());

Paint = new Paint ();
Paint. setStyle (Paint. Style. STROKE );
Paint. setXfermode (new porterduxfermode (Mode. CLEAR ));
Paint. setAntiAlias (true );
Paint. setDither (true );
Paint. setStrokeJoin (Paint. Join. ROUND );
Paint. setStrokeCap (Paint. Cap. ROUND );
Paint. setStrokeWidth (20 );

Path = new Path ();

MCanvas = new Canvas (bitmap );
MCanvas. drawBitmap (frontBitmap, 0, 0, null );
}

@ Override
Public boolean onTouchEvent (MotionEvent event ){
Float ax = event. getX ();
Float ay = event. getY ();

If (event. getAction () = MotionEvent. ACTION_DOWN ){
IsMove = false;
Path. reset ();
Path. moveTo (ax, ay );
Invalidate ();
Return true;
} Else if (event. getAction () = MotionEvent. ACTION_MOVE ){
IsMove = true;
Path. lineTo (ax, ay );
Invalidate ();
Return true;
}
Return super. onTouchEvent (event );
}

Public Bitmap CreateBitmap (int color, int width, int height ){
Int [] rgb = new int [width * height];

For (int I = 0; I <rgb. length; I ++ ){
Rgb [I] = color;
}

Return Bitmap. createBitmap (rgb, width, height, Config. ARGB_8888 );
}

}

Layout File

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

<RelativeLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_marginLeft = "30dp"
Android: layout_marginTop = "100dp">

<TextView
Android: id = "@ + id/TV _main_msg"
Android: layout_width = "300dp"
Android: layout_height = "150dp"
Android: background = "@ android: color/holo_red_dark"
Android: gravity = "center_vertical | center_horizontal"
Android: text = "You did not win"
Android: textSize = "40sp"/>

<Com. example. guale. EraseView
Android: id = "@ + id/eraseView1"
Android: layout_width = "300dp"
Android: layout_height = "150dp"/>
</RelativeLayout>

</LinearLayout>

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.