Code Implementation 9patch format

Source: Internet
Author: User
Package com. himi; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. ninePatch; import android. graphics. paint; import android. graphics. rectF; import android. util. log; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. surface Holder. callback; public class MySurfaceView extends SurfaceView implements Callback, Runnable {private Thread th = new Thread (this); private SurfaceHolder sfh; private Canvas canvas; private Paint; private Bitmap BMP _old; private Bitmap BMP _9path; private NinePatch np; public MySurfaceView (Context context) {super (context); this. setKeepScreenOn (true); BMP _old = BitmapFactory. decodeResource (GetResources (), R. drawable. himi_old); BMP _9path = BitmapFactory. decodeResource (getResources (), R. drawable. himi_9path); np = new NinePatch (BMP _9path, BMP _9path.getninepatchchunk (), null); // create a ninePatch object instance. The first parameter is bitmap, and the second parameter is byte []. in fact, we are required to pass in the // processing formula. Of course, we will not just upload it, because the .9.png image itself contains the information data, // that is, the information we use the "9 Mei" tool to operate! You can call the getNinePatchChunk () parameter to directly use the data of the ".9.png image. // The third parameter is the image source name. This parameter is optional and is directly null ~ OK ~ Sfh = this. getHolder (); sfh. addCallback (this); paint = new Paint (); paint. setAntiAlias (true); setFocusable (true);} public void surfaceCreated (SurfaceHolder holder) {Log. v ("Himi", "surfaceCreated"); th. start ();}/*** @ author Himi */public void draw () {canvas = sfh. lockCanvas (); canvas. drawColor (Color. BLACK); RectF rectf_old_two = new RectF (0, 50, BMP _old.getwidth () * 2,120 + BMP _old.getheight () * 2 ); // Note 1 RectF rectf_old_third = new RectF (0,120 + BMP _old.getheight () * 2, BMP _old.getwidth () * 3,140 + BMP _old.getheight () * 2 + BMP _old.getheight () * 3 ); // -------- The following is a normal png painting method ----------- canvas. drawBitmap (BMP _old, 0, 0, paint); canvas. drawBitmap (BMP _old, null, rectf_old_two, paint); canvas. drawBitmap (BMP _old, null, painting, paint); RectF rectf_9path_two = new RectF (250, 50,250 + BMP _9path.getwidth () * 2, 90 + BMP _9path.getheight () * 2 ); rectF Signature = new RectF (250,120 + BMP _9path.getheight () * 2,250 + BMP _9path.getwidth () * 3,140 + BMP _9path.getheight () * 2 + BMP _9path.getheight () * 3); canvas. drawBitmap (BMP _9path, 250, 0, paint); // -------- The following is the painting method of ".9.png" image ----------- np. draw (canvas, rectf_9path_two); np. draw (canvas, rectf_9path_third); sfh. unlockCanvasAndPost (canvas);} public void run () {// TODO Auto-generated method stub while (true) {draw (); try {Thread. sleep (100);} catch (Exception ex) {}} public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {Log. v ("Himi", "surfaceChanged");} public void surfaceDestroyed (SurfaceHolder holder) {Log. v ("Himi", "surfaceDestroyed ");}}

.9.png file features

Compared with a traditional png image, a 9.png image consists of an edge of one pixel around the image. This edge is used to define the expandable exhibition area and content display area of the image.

Images in this format can be adjusted automatically in the android environment.

(1) developers are allowed to define extensible areas. When you need to extend an image to fill a region larger than the image itself, the content of the expanded area is extended.

(2) allow developers to define content display areas for displaying text or other content

As shown in:

The black lines on the left and above can be expanded.

The black line on the right and bottom is the content display area.

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.