Android Learning notes Advanced 16 Bitmapshader

Source: Internet
Author: User

<1> Introduction

Take a look at the blog post: Android Learning notes advanced 15 shader rendering

Public Bitmapshader(Bitmap bitmap,shader.tilemode tilex,shader.tilemode Tiley)

Call this method to produce a renderer (Shader) that draws a bitmap.

Bitmap bitmaps used within the renderer

Tilex the tiling mode for x to draw the bitmap in. X-Directional tile pattern on the bitmap

Tiley the tiling mode for Y-draw the bitmap in. Y-Direction tile pattern on the bitmap

Tilemode: (a total of three kinds)

CLAMP: If the renderer exceeds the original boundary range, the range edge staining is copied.

REPEAT: Landscape and portrait Repeat renderer picture, tiled.

MIRROR: Horizontal and vertical duplicate renderer picture, this is not the same as the REPEAT repetition, he is tiled in a mirrored manner.

Still don't understand? Let's see it!

REPEAT MIRROR

<2> Concrete Implementation

[Java]View PlainCopy
  1. Package Xiaosi. Bitmapshader;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Public class Bitmapshaderactivity extends Activity {
  5. /** Called when the activity is first created. * /
  6. private Bitmapshaders bitmapshaders = null;
  7. @Override
  8. public void OnCreate (Bundle savedinstancestate) {
  9. super.oncreate (savedinstancestate);
  10. Bitmapshaders = New Bitmapshaders (this);
  11. Setcontentview (bitmapshaders);
  12. }
  13. }


Bitmapshaders.java

[Java]View PlainCopy
  1. Package Xiaosi. Bitmapshader;
  2. Import Android.content.Context;
  3. Import Android.graphics.Bitmap;
  4. Import Android.graphics.BitmapShader;
  5. Import Android.graphics.Canvas;
  6. Import Android.graphics.Paint;
  7. Import Android.graphics.Shader;
  8. Import android.graphics.drawable.BitmapDrawable;
  9. Import android.graphics.drawable.ShapeDrawable;
  10. Import Android.graphics.drawable.shapes.OvalShape;
  11. Import Android.view.View;
  12. Public class Bitmapshaders extends View
  13. {
  14. private Bitmapshader bitmapshader = null;
  15. private Bitmap Bitmap = null;
  16. Private Paint paint = null;
  17. private Shapedrawable shapedrawable = null;
  18. private int bitmapwidth = 0;
  19. private int bitmapheight = 0;
  20. Public bitmapshaders (context context)
  21. {
  22. super (context);
  23. //Get images
  24. Bitmap = ((bitmapdrawable) getresources (). getdrawable (R.drawable.h)). Getbitmap ();
  25. Bitmapwidth = Bitmap.getwidth ();
  26. Bitmapheight = Bitmap.getheight ();
  27. //Construct renderer Bitmapshader
  28. Bitmapshader = new Bitmapshader (bitmap,shader.tilemode.mirror,shader.tilemode.repeat);
  29. }
  30. @Override
  31. protected void OnDraw (canvas canvas)
  32. {
  33. Super.ondraw (canvas);
  34. //Crop the picture to an ellipse
  35. //Build Shapedrawable object and define shape as ellipse
  36. shapedrawable = New Shapedrawable (new OvalShape ());
  37. //Get brushes and set renderer
  38. Shapedrawable.getpaint (). Setshader (Bitmapshader);
  39. //Set display area
  40. Shapedrawable.setbounds ( 20,bitmapwidth-60,bitmapheight-60);
  41. //Draw shapedrawable
  42. Shapedrawable.draw (canvas);
  43. }
  44. }

Android Learning notes Advanced 16 Bitmapshader

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.