栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

motu-funtion

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

motu-funtion

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 = 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 (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();

    }

}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号