自定義view與自定義drawable在設定影象中的使用
阿新 • • 發佈:2019-02-19
自定義View
先使用BitmapFactory.decodeResource()來獲取一個位圖Bitmap,其中第一個引數為資源,第二個引數為要修改的圖片
然後使用Shader對其進行操作
對於BitmapShader有幾種模式{CLAMP拉伸,REPEAT重複,MIRROR映象},這裡選CLAMP
然後將獲得的bitmap繪製成圓形
這樣便可以在佈局中使用這個view了
程式碼如下
import android.view.View; import com.example.administrator.copyqq01.R; /** * Created by Administrator on 2016/6/7. * 實現image的圓形顯示 */ public class ImageViewRound extends View { private Bitmap bitmap; private Paint paint; private BitmapShader bitmapShader; public ImageViewRound(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public ImageViewRound(Context context, AttributeSet attrs) { super(context, attrs); } public ImageViewRound(Context context) { super(context); } //如何動態的載入R.drawable.get2資源? @Override protected void onDraw(Canvas canvas) { bitmap= BitmapFactory.decodeResource(getResources(), R.drawable.get2); bitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP); paint=new Paint(); paint.setShader(bitmapShader); canvas.drawCircle(50,50,50,paint); } }
自定義Drawable
繼承一個Drawable,在其中的建構函式中對傳入的Bitmap進行設定,在佈局中加入一個ImageView(或者其他什麼的),然後在程式碼中對imageView進行設定
import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.PixelFormat; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.drawable.Drawable; /** * Created by Administrator on 2016/7/3. */ public class RoundImageDrawable extends Drawable { private Paint mPaint; private Bitmap mBitmap; private RectF rectF; public RoundImageDrawable(Bitmap bitmap){ mBitmap=bitmap; BitmapShader bitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mPaint=new Paint(); mPaint.setAntiAlias(true);//抗鋸齒 mPaint.setShader(bitmapShader); } @Override public void setBounds(int left,int top,int right,int bottom) { super.setBounds(left,top,right,bottom); rectF=new RectF(left,top,right,bottom); } @Override public void draw(Canvas canvas) { canvas.drawRoundRect(rectF,30,30,mPaint); } @Override public int getIntrinsicHeight() { return mBitmap.getHeight(); } @Override public int getIntrinsicWidth() { return mBitmap.getWidth(); } @Override public void setAlpha(int alpha) { mPaint.setAlpha(alpha); } @Override public void setColorFilter(ColorFilter colorFilter) { mPaint.setColorFilter(colorFilter); } @Override public int getOpacity() { return PixelFormat.TRANSLUCENT;//設定透明 } }
Mainactivity中設定
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ImageView; /** * Created by Administrator on 2016/3/7. */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.mv); ImageView imageView=(ImageView)findViewById(R.id.id_one); imageView.setImageDrawable(new RoundImageDrawable(bitmap)); } }
參考