Android學習筆記進階16之BitmapShader

來源:互聯網
上載者:User

<1>簡介

具體的看一下博文:Android學習筆記進階15之Shader渲染

 

public   BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)

調用這個方法來產生一個畫有一個位元影像的渲染器(Shader)。

bitmap   在渲染器內使用的位元影像

tileX      The tiling mode for x to draw the bitmap in.   在位元影像上X方向花磚模式

tileY     The tiling mode for y to draw the bitmap in.    在位元影像上Y方向花磚模式

 

TileMode:(一共有三種)

CLAMP  :如果渲染器超出原始邊界範圍,會複製範圍內邊緣染色。

REPEAT :橫向和縱向的重複渲染器圖片,平鋪。

MIRROR :橫向和縱向的重複渲染器圖片,這個和REPEAT 重複方式不一樣,他是以鏡像方式平鋪。

還是不太明白?那看一下吧!

                                       REPEAT                                                                                                                       MIRROR

<2>具體實現

 

package xiaosi.BitmapShader;</p><p>import android.app.Activity;<br />import android.os.Bundle;</p><p>public class BitmapShaderActivity extends Activity {<br /> /** Called when the activity is first created. */<br />private BitmapShaders bitmapShaders = null;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);</p><p> bitmapShaders = new BitmapShaders(this);<br /> setContentView(bitmapShaders);<br /> }<br />}

 

BitmapShaders.java

package xiaosi.BitmapShader;</p><p>import android.content.Context;<br />import android.graphics.Bitmap;<br />import android.graphics.BitmapShader;<br />import android.graphics.Canvas;<br />import android.graphics.Paint;<br />import android.graphics.Shader;<br />import android.graphics.drawable.BitmapDrawable;<br />import android.graphics.drawable.ShapeDrawable;<br />import android.graphics.drawable.shapes.OvalShape;<br />import android.view.View;</p><p>public class BitmapShaders extends View<br />{<br /> private BitmapShader bitmapShader = null;<br /> private Bitmap bitmap = null;<br /> private Paint paint = null;<br /> private ShapeDrawable shapeDrawable = null;<br /> private int BitmapWidth = 0;<br /> private int BitmapHeight = 0;<br />public BitmapShaders(Context context)<br />{<br />super(context);<br />//得到映像<br />bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.h)).getBitmap();<br />BitmapWidth = bitmap.getWidth();<br />BitmapHeight = bitmap.getHeight();<br />//構造渲染器BitmapShader<br />bitmapShader = new BitmapShader(bitmap,Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);<br />}<br />@Override<br />protected void onDraw(Canvas canvas)<br />{<br />super.onDraw(canvas);<br />//將圖片裁剪為橢圓形<br /> //構建ShapeDrawable對象並定義形狀為橢圓<br />shapeDrawable = new ShapeDrawable(new OvalShape());<br />//得到畫筆並設定渲染器<br />shapeDrawable.getPaint().setShader(bitmapShader);<br />//設定顯示地區<br />shapeDrawable.setBounds(20, 20,BitmapWidth-60,BitmapHeight-60);<br />//繪製shapeDrawable<br />shapeDrawable.draw(canvas);<br />}<br />}<br />

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.