本篇重点是: 根据设置的图片轨迹之上画上去资源图片.
比如下面: 可以看到他的框架是六角形.那么放上去图片时会根据那个轨迹贴上去.
研究一下网址.对理解有帮助
http://blog.csdn.net/harvic880925/article/details/51264653
核心代码是:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
使用方法:
其中setImageFrame是 setImageMask是轨迹用的框.
setImageFrame是 边框, setImageResource是要根据轨迹画上去的 资源图片
mIvTeamImage.setImageMask(R.drawable.join_a_team_teamlogo_frame);//// TODO: 2017. 5. 24. 이미지를 올려놓아야 한다mIvTeamImage.setImageFrame(R.drawable.bg_team_image_324_362);
mIvTeamImage.setImageResource(R.drawable.teamlogo_no_img);
贴出全部代码.
public class MaskImage extends ImageView { Bitmap mMask = null; Bitmap mFrame = null; int mFrameResId = -1;
Bitmap mOriBmp = null; //Bitmap mResult; private int m_nWidth = -1; private int m_nHeight = -1; private int m_nResId = -1; private String m_szPath = null; private boolean m_bNeedRemask = false;
private Canvas mCanvas = null;
public MaskImage(Context context) { super(context);
}
public MaskImage(Context context, AttributeSet attrs) { super(context, attrs); }
public MaskImage(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
public void setImageMask(int resource) { if(mMask != null) { mMask.recycle(); } mMask = BitmapFactory.decodeResource(getResources(), resource); }
public void setImageFrame(int resource) { if(mFrameResId == resource) return;
if(mFrame != null) { mFrame.recycle(); } if (resource == 0) { mFrame = null; mFrameResId = -1; return; } mFrameResId = resource; mFrame = BitmapFactory.decodeResource(getResources(), resource); m_bNeedRemask = true; }
@Override public void setImageResource(final int resId) { if(m_nResId == resId) { return; } m_bNeedRemask = true; m_nResId = resId; m_szPath = null; mOriBmp = null; invalidate(); }
@Override public void setImageBitmap(Bitmap bm) { mOriBmp = bm; m_nResId = -1; m_szPath = null; m_bNeedRemask = true; invalidate(); }
public void setImagePath(String imagePath) { if(m_szPath != null && m_szPath.equals(imagePath)) { return; }
mOriBmp = null; m_nResId = -1; m_szPath = imagePath; m_bNeedRemask = true; invalidate(); }
private void createMaskBitmap(Bitmap original) { if(mMask != null) { if(mMask.getWidth() != m_nWidth || mMask.getHeight() != m_nHeight) { Bitmap bmp = Bitmap.createScaledBitmap(mMask, m_nWidth, m_nHeight, true); mMask.recycle(); mMask = bmp; } }
if(mFrame != null) { if(mFrame.getWidth() != m_nWidth || mFrame.getHeight() != m_nHeight) { Bitmap bmp = Bitmap.createScaledBitmap(mFrame, m_nWidth, m_nHeight, true); mFrame.recycle(); mFrame = bmp; } }
if(original != null) { if(original.getWidth() != m_nWidth || original.getHeight() != m_nHeight) { Bitmap bmp = Bitmap.createScaledBitmap(original, m_nWidth, m_nHeight, true); original.recycle(); original = bmp; } }
Bitmap bitmap = Bitmap.createBitmap(m_nWidth, m_nHeight, Config.ARGB_8888); Canvas mCanvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); if(original != null) { mCanvas.drawBitmap(original, 0, 0, null); } mCanvas.drawBitmap(mMask, 0, 0, paint); paint.setXfermode(null); if (null != mFrame) { mCanvas.drawBitmap(mFrame, 0, 0, paint); } super.setImageBitmap(bitmap); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if(changed && m_nWidth == -1) { m_nWidth = right - left; m_nHeight = bottom - top; } }
@Override protected void onDraw(Canvas canvas) { if(m_bNeedRemask) { Bitmap bmp = null; if(m_nResId != -1) { bmp = BitmapFactory.decodeResource(getResources(), m_nResId); } else if(m_szPath != null) { bmp = BitmapFactory.decodeFile(m_szPath); } else if(mOriBmp != null) { bmp = mOriBmp; } createMaskBitmap(bmp); m_bNeedRemask = false; } super.onDraw(canvas); }}