package com.diction.app.android._av7.motu;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.diction.app.android._av7._view.utils.PrintUtilsJava;
public class PartView extends View {
private int width, height;
private Paint paint;
private Bitmap bitmapBottom;
private PorterDuffXfermode porterDuffXfermode;
private float mScaleF;
private Bitmap bitmapMask;
private BitmapShader bitmapShader;
private int initX = 0;
private int initY = 0;
private Matrix matrix;
private float mInScale = 0.4f;
public String partId;
public static final float colormatrix_huajiu[] = {
1f, 0, 0, 0, 0,
0, 2f, 0, 0, 0,
0, 0, 1f, 0, 0,
0, 0, 0, 1f, 0};
public String mBottmonResource;
public int mColorFilter = -1;
public String mBgImagePath;
public String getPartId() {
return partId;
}
public PartView(Context context) {
this(context, null);
}
public PartView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public PartView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context text) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setDither(true);
paint.setFilterBitmap(true);
mScaleF = 0.4f;
initPor();
matrix = new Matrix();
matrix.setScale(0.4f, 0.4f);
// paint.setColorFilter(new PorterDuffColorFilter(Color.RED,PorterDuff.Mode.ADD));
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
public void setBitmapInfo(String bgImg, String id, int mMotuWhdth, int mMotuHeight, int centerX, int centerY) {
width = mMotuWhdth;
height = mMotuHeight;
partId = id;
mBgImagePath = bgImg;
PrintUtilsJava.pringtLog("setBitmapInfo--center->" + centerX + " " + centerY);
Bitmap bitmapResouce = BitmapFactory.decodeFile(bgImg);
if (bitmapResouce != null) {
//防止变形,重新计算尺寸
int reaLwidth = bitmapResouce.getWidth();
int reaLheight = bitmapResouce.getHeight();
if (reaLwidth>=reaLheight){
if (height>width){
height = (int) (width*reaLheight/(reaLwidth*1.0f));
}
}else{
width = (int)(height *reaLwidth /(reaLheight*1.0f));
}
PrintUtilsJava.pringtLog("MoTuMaskView--->reqal = "+ height +" "+ width +" "+ reaLheight+" "+ reaLwidth);
/* initX = centerX * width / bitmapResouce.getWidth();
initY = centerY * height / bitmapResouce.getHeight(); */
initX = mMotuWhdth / 2;
initY = mMotuHeight / 2;
PrintUtilsJava.pringtLog("setBitmapInfo--initcente->" + initX + " " + initY);
bitmapMask = Bitmap.createScaledBitmap(bitmapResouce, width, height, true);
invalidate();
}
}
public void setBottomImage(String btoImage) {
Bitmap bitmapResouce = BitmapFactory.decodeFile(btoImage);
if (bitmapResouce != null) {
mBottmonResource = btoImage;
paint.setXfermode(null);
paint.setShader(null);
bitmapBottom = Bitmap.createScaledBitmap(bitmapResouce, width, height, true);
bitmapShader = new BitmapShader(bitmapBottom, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
// if (porterDuffXfermode == null) {
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
// }
if (paint == null) {
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setDither(true);
paint.setFilterBitmap(true);
}
// bitmapBottom.setColorF/
// invalidate();
/* //重新初始化一下
if (!TextUtils.isEmpty(mBgImagePath)){
Bitmap maskBg = BitmapFactory.decodeFile(mBgImagePath);
if (maskBg != null) {
//防止变形,重新计算尺寸
int reaLwidth = bitmapResouce.getWidth();
int reaLheight = bitmapResouce.getWidth();
if (reaLwidth>=reaLheight){
if (height>width){
height = width*reaLheight/reaLwidth;
}
}else{
width = height *reaLwidth /reaLheight;
}
*//* initX = centerX * width / bitmapResouce.getWidth();
initY = centerY * height / bitmapResouce.getHeight(); *//*
initX = width / 2;
initY = height / 2;
PrintUtilsJava.pringtLog("setBitmapInfo--initcente->" + initX + " " + initY);
bitmapMask = Bitmap.createScaledBitmap(maskBg, width, height, true);
}
}*/
if (mColorFilter != -1){
int red = (mColorFilter & 0xff0000) >> 16;
int green = (mColorFilter & 0x00ff00) >> 8;
int blue = (mColorFilter & 0x0000ff);
float[] colorMatrix = {
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 1f, 0f,
};
colorMatrix[0] = red/255f;
colorMatrix[6] = green/255f;
colorMatrix[12] = blue/255f;
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
}
postInvalidate();
}
PrintUtilsJava.pringtLog("setBottomImage--->" + btoImage + " bitmapResouce =" + bitmapResouce);
}
private void initPor() {
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("initx01", "initx-->bitmapMask " + bitmapBottom + " bitmapMask " + bitmapMask);
if (bitmapBottom == null || bitmapMask == null) {
Log.e("initx02", "initx-->bitmapMask " + bitmapBottom + " bitmapMask " + bitmapMask);
return;
}
if (porterDuffXfermode == null) {
porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
}
// rectF.top = currentTop;
PrintUtilsJava.pringtLog("onDraw---01 >" + mBottmonResource+ " " +mBgImagePath);
int sc = canvas.saveLayer(0, 0,
width, height, null, Canvas.ALL_SAVE_FLAG);
Log.e("initx", "initx-->" + initX + " " + initY);
matrix.setScale(mInScale, mInScale, width / 2 + initX, height / 2 + initY);
// matrix.postTranslate(initX, initY);
bitmapShader.setLocalMatrix(matrix);
paint.setShader(bitmapShader);
canvas.drawRect(0, 0, width, height, paint);
canvas.drawBitmap(bitmapBottom, matrix, paint);
paint.setXfermode(porterDuffXfermode);
PrintUtilsJava.pringtLog("#8* 02020");
canvas.drawBitmap(bitmapMask, 0, 0, paint);
paint.setShader(null);
paint.setXfermode(null);
canvas.restoreToCount(sc);
PrintUtilsJava.pringtLog("onDraw---01 >" + mBottmonResource+ " " +mBgImagePath);
//
}
public void resetColorFilter(){
if (paint != null){
paint.setColorFilter(null);
Log.e("setScaleFloat", "resetColorFilter----");
postInvalidate();
}
}
public void setScaleFloat(float scaleSize) {
Log.e("setScaleFloat", "setScaleFloat----" + scaleSize);
mInScale = scaleSize;
postInvalidate();
}
public void setColorFilter(int color) {
int red = (color & 0xff0000) >> 16;
int green = (color & 0x00ff00) >> 8;
int blue = (color & 0x0000ff);
float[] colorMatrix = {
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 1f, 0f,
};
colorMatrix[0] = red/255f;
colorMatrix[6] = green/255f;
colorMatrix[12] = blue/255f;
Log.e("setColorFilter","colorMatri 0--->"+red/255f );
Log.e("setColorFilter","colorMatri 1--->"+green/255f );
Log.e("setColorFilter","colorMatri 2--->"+blue/255f );
if (TextUtils.isEmpty(mBottmonResource)){
bitmapBottom = Bitmap.createBitmap(bitmapMask.getWidth(), bitmapMask.getHeight(),
Bitmap.Config.ARGB_8888);
bitmapBottom.eraseColor(color);//填充颜色
// ColorDrawable drawable = new ColorDrawable(Color.parseColor("#6B61BA7F"));
Canvas canvas = new Canvas(bitmapBottom);
bitmapShader = new BitmapShader(bitmapBottom, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
}else{
paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
}
mColorFilter= color;
postInvalidate();
}
}