刮刮乐效果、生成图片文件、在XML

前端之家收集整理的这篇文章主要介绍了刮刮乐效果、生成图片文件、在XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

效果展示:

直接上代码

package com.example.administrator.mybitmap.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.administrator.mybitmap.R;

/** * Created by Administrator on 2015/9/17. */
public class MyBitmap2 extends View {
    private int width;
    private int height;
    private Paint mPaintCircle;
    private Paint mPaintRect;
    private Paint mPaintBackground;
    private Bitmap bitmap;
    private Bitmap bitmapback;
    private Matrix matrix;

    public MyBitmap2(Context context) {
        super(context);
    }

    public MyBitmap2(Context context,AttributeSet attrs) {
        super(context,attrs);
        final TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyBitmap2);
        BitmapDrawable dra = (BitmapDrawable) a.getDrawable(R.styleable.MyBitmap2_myview_background);
        if (dra != null) {
            bitmapback = dra.getBitmap();
        } else {
            bitmapback = BitmapFactory.decodeResource(getResources(),R.mipmap.lol);
        }
        int paintWidth = a.getDimensionPixelOffset(R.styleable.MyBitmap2_myview_paint,70);

        mPaintCircle = new Paint();
        mPaintCircle.setColor(Color.BLUE);
        mPaintRect = new Paint();
        mPaintRect.setColor(Color.GREEN);
        mPaintCircle = new Paint();
        mPaintCircle.setColor(Color.YELLOW);
        //设置图形的交互模式
        PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.XOR);
        mPaintRect.setXfermode(mode);
        mPaintRect.setStyle(Paint.Style.STROKE);
        mPaintRect.setStrokeJoin(Paint.Join.ROUND); // 连接点处样式圆角
        mPaintRect.setStrokeCap(Paint.Cap.ROUND); // 覆盖时的样式.圆角
        mPaintRect.setStrokeWidth(paintWidth);
        matrix = new Matrix();
        // 设置画笔宽度
// mPaintRect.setStrokeWidth(20);
// bitmapback = BitmapFactory.decodeResource(getResources(),R.mipmap.lol);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
        height = getDefaultSize(getSuggestedMinimumHeight(),heightMeasureSpec);
        setMeasuredDimension(width,height);
        bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(bitmap);
        mPath = new Path();
        matrix.reset();
        matrix.postScale((width + 0.0f) / bitmapback.getWidth(),(height + 0.0f) / bitmapback.getHeight());
    }

    private Path mPath;
    private Canvas mCanvas;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //矩阵的方法来放大图片
        canvas.drawBitmap(bitmapback,matrix,null);
        // 用裁剪的方法放大图片
        // canvas.drawBitmap(bitmapback
        //,new Rect(0,bitmapback.getWidth(),bitmapback.getHeight())
        //,width,height),null);


        mCanvas.drawRect(0,0,mPaintCircle);
        mCanvas.drawPath(mPath,mPaintRect);//xor效果的笔
        canvas.drawBitmap(bitmap,null);
    }

    float x;
    float y;
    float new_x;
    float new_y;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                x = event.getX();
                y = event.getY();
                mPath.moveTo(x,y);
                break;
            case MotionEvent.ACTION_MOVE:
                new_x = event.getX();
                new_y = event.getY();

                mPath.lineTo(new_x,new_y);
                x = new_x;
                y = new_y;


// mPath.addCircle(x,y,50,Path.Direction.CW);//在点击出画一个圆,

                break;
        }
        invalidate();
        return true;

    }
}

MainActivity

package com.example.administrator.mybitmap;

import android.graphics.Bitmap;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.mybitmap.widget.MyBitmap2;
import com.example.administrator.mybitmap.widget.MySlider;

import org.w3c.dom.Text;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private MySlider mySlider;
    private TextView mTextView;
    private Button mBtnToBePicture;
 private MyBitmap2 myBitmap2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitmap2);
        mBtnToBePicture= (Button) findViewById(R.id.btn_tobepicture);
        myBitmap2= (MyBitmap2) findViewById(R.id.myview_bitmap2);
        /** *生成图片文件方法 */
        mBtnToBePicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myBitmap2.setDrawingCacheEnabled(true);
                Bitmap bitmap=myBitmap2.getDrawingCache(true);
                File file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
                if(!file.exists()){

                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                try {
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(file));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });

// mySlider = (MySlider) findViewById(R.id.view_slider);
// mTextView = (TextView) findViewById(R.id.textview);
// mySlider.setonItemSelectLinser(new MySlider.onItemSelect() {
// @Override
// public void onItemSelected(int index,String nameString) {
// if(index!=-1){
// mTextView.setText(nameString);
// mTextView.setVisibility(View.VISIBLE);
// }
// }
// });
    }


}

布局文件activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myview_values="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
    <Button  android:id="@+id/btn_tobepicture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="生成图片"/>
    <com.example.administrator.mybitmap.widget.MyBitmap2  android:id="@+id/myview_bitmap2" android:layout_width="match_parent" android:layout_height="match_parent" myview_values:myview_background="@mipmap/an" myview_values:myview_paint="40dp" />

</LinearLayout>

myview_values中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyBitmap2">
        <attr name="myview_background" format="reference"></attr>//设置背景
        <attr name="myview_paint" format="dimension|reference"></attr>//dimension表示可以是xx dp sp等,reference指可以获取res下的资源
    </declare-styleable>
</resources>
原文链接:https://www.f2er.com/xml/296112.html

猜你在找的XML相关文章