Simple canvas for Android

Source: Internet
Author: User


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + tq + 7 rcbkyrW + release + m8/snPu + release/release + yc + 75tbGtcTNvNDOt6LJ + release/vMfXodXi0KnK/release + vODM/release + /kernel + CjxwPs/kernel + jrMO/zPXWsc/kernel/Kwrz + kernel/VBhdGjA4L/kernel + kernel/kernel + ckernel vcd4kpha + kernel = "brush: java; "> 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. util. attributeSet; import android. view. motionEvent; import android. view. view; public class DrawView extends View {float preX; float preY; private Path path; public Paint paint = null; final int VIEW_WIDTH = 480; // you can adjust the final int VIEW_HEIGHT = 800 according to the resolution of your mobile phone; // define an image in memory, which will be used as the buffer zone Bitmap cacheBitmap = null; // define the Canvas object Canvas cacheCanvas on cacheBitmap = null; public DrawView (Context context, AttributeSet set) {super (context, set ); // create a cache with the same size as this View. cacheBitmap = Bitmap. createBitmap (VIEW_WIDTH, VIEW_HEIGHT, Config. ARGB_8888); cacheCanvas = new Canvas (); path = new Path (); // set cacheCanvas to be drawn to cacheBitmap in memory on cacheCanvas. setBitmap (cacheBitmap); // set the paint color to Paint = new Paint (paint. DITHER_FLAG); paint. setColor (Color. RED); // set the paint style paint. setStyle (Paint. style. STROKE); paint. setStrokeWidth (1); // anti-sawtooth paint. setAntiAlias (true); paint. setDither (true) ;}@ Overridepublic boolean onTouchEvent (MotionEvent event) {// gets the position where the drag event occurs float x = event. getX (); float y = event. getY (); switch (event. getAction () {case MotionEvent. ACTION_DOWN: // manually press path. moveTo (x, y); preX = x; preY = y; break; case MotionEvent. ACTION_MOVE: // manually move the path. quadTo (preX, preY, x, y); preX = x; preY = y; break; case MotionEvent. ACTION_UP: // release cacheCanvas. drawPath (path, paint); path. reset (); break;} invalidate (); // return true indicating that the processing method has handled the event return true;} @ Overridepublic void onDraw (Canvas canvas) {Paint BMP Paint = new Paint (); // draws cacheBitmap to the View component canvas. drawBitmap (cacheBitmap, 0, 0, BMP paint); // draw a canvas along the path. drawPath (path, paint );}}

Import android. app. activity; import android. graphics. blurMaskFilter; import android. graphics. color; import android. graphics. embossMaskFilter; import android. OS. bundle; import android. view. menu; import android. view. menuInflater; import android. view. menuItem; public class HandDraw extends Activity {/* The MaskFilter class can assign edge effects to the Paint. The extension of MaskFilter can be applied to the alpha channel of a Paint edge. Android includes the following maskfilters: BlurMaskFilter specifies a blur style and radius to process the painting edge. EmbossMaskFilter specifies the direction of the light source and the ambient light intensity to add relief effects. To apply a MaskFilter, you can use the setMaskFilter method and pass it a MaskFilter object. */EmbossMaskFilter emboss; BlurMaskFilter blur; @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); emboss = new EmbossMaskFilter (new float [] {1.5f, 1.5f, 1.5f}, 0.6f, 6, 4.2f); blur = new BlurMaskFilter (8, BlurMaskFilter. blur. NORMAL) ;}@ Override // creates the option Menu public boolean onCreateOptionsMenu (menu Menu) {MenuInflater inflator = new Men UInflater (this); // load R. menu. the menu corresponding to my_menu and add it to inflator in menu. inflate (R. menu. my_menu, menu); return super. onCreateOptionsMenu (menu) ;}@ Override // callback method after the menu item is clicked public boolean onOptionsItemSelected (MenuItem mi) {DrawView dv = (DrawView) findViewById (R. id. draw); // determines which menu item is clicked and responds accordingly. Switch (mi. getItemId () {case R. id. red: dv. paint. setColor (Color. RED); mi. setChecked (true); break; case R. id. green: dv. paint. setColor (Color. GREEN); mi. setChecked (true); break; case R. id. blue: dv. paint. setColor (Color. BLUE); mi. setChecked (true); break; case R. id. width_1: dv. paint. setStrokeWidth (1); break; case R. id. width_3: dv. paint. setStrokeWidth (3); break; case R. id. width_5: dv. paint. setStrokeWidth (5); break; case R. id. blur: dv. paint. setMaskFilter (blur); break; case R. id. emboss: dv. paint. setMaskFilter (emboss); break;} return true ;}}

 
 
  
   
    
    
     
     
     
     
    
   
  
  
   
    
    
     
     
     
     
    
   
  
  
  
 


Related Article

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.