如何在imageview的同一区域填充不同的颜色(在imageview的同一区域上的另一种颜色的颜色)?

时间:2014-09-10 06:00:46

标签: android canvas imageview paint

我有一个imageview,我设置图像以填充不同的颜色。但是当我填充颜色时,其他颜色不会填充在imageview上相同的区域,而前一种颜色被填充。我使用颜色托盘按钮获取不同颜色以填充imageview。以下是我的代码和图片说明。

public class Play_Screen extends Activity implements OnTouchListener,
        OnClickListener {



     private Thread thread;
        private boolean running = false;

//  DrawImageView iv;
    // ImageView imageView;
    private Context context = this;

    private int paintAlpha = 255;

    // initial color
    private int paintColor;

    MediaPlayer playsound = null;

    SeekBar seekBar1;
    Button btn_paintbrush, btn_home, btn_cam, btn_cancel, btn_undo, btn_redo,
            btn_eraser, btn_prev, btn_next, btn_color_one, btn_color_two,
            btn_color_three, btn_color_four, btn_color_five, btn_color_six,
            btn_color_seven, btn_color_eight, btn_color_nine, btn_color_ten,
            btn_color_elevan, btn_color_twelve, btn_color_thirteen,
            btn_color_fourteen, btn_color_fifteen, btn_color_sixteen;
    ImageView image;
    Bitmap bitmap,bitmap2;
    Bitmap bmp;
    Canvas canvas;
    Paint paint;
    float downx = 0, downy = 0, upx = 0, upy = 0;

    RelativeLayout relative;

    SharedPreferences prefs;

    private boolean erase = false;
    // custom drawing view

    public static int DARK_PINK = Color.argb(255, 255, 51, 255);
    public static int LIGHT_YELLOW = Color.argb(255, 255, 230, 102);
    public static int DARK_MARUN = Color.argb(255, 148, 66, 50);
    public static int LIGHT_MARUN = Color.argb(255, 186, 123, 68);
    public static int RED = Color.argb(255, 252, 20, 20);
    public static int LIGHT_BLUE = Color.argb(255, 102, 255, 255);
    public static int DARK_BLUE = Color.argb(255, 70, 78, 202);
    public static int LIGHT_GREEN = Color.argb(255, 190, 255, 91);
    public static int DARK_GREEN = Color.argb(255, 15, 230, 0);
    public static int JAMBLI = Color.argb(255, 123, 0, 230);
    public static int ORANGE = Color.argb(255, 255, 187, 50);
    public static int BLACK = Color.argb(255, 7, 5, 0);
    public static int GRAY = Color.argb(255, 129, 128, 127);
    public static int PINK_RED = Color.argb(255, 255, 4, 139);
    public static int NEAVYBLUE = Color.argb(255, 51, 204, 255);
    public static int ADVANCE_GREEN = Color.argb(255, 102, 255, 204);

    private Path path = new Path();

    private ArrayList<Path> undonePaths = new ArrayList<Path>();
    private ArrayList<Path> paths = new ArrayList<Path>();
    int strok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.play_screen);

        Bundle extras = getIntent().getExtras();
        byte[] byteArray = extras.getByteArray("picture");

        bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);


        relative = (RelativeLayout)findViewById(R.id.relative);
        image = (ImageView) findViewById(R.id.imageView1);

        // image.setImageBitmap(bmp);
        // image.setOnTouchListener(this);



        seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
        seekBar1.setVisibility(View.INVISIBLE);

        strok = GlobalVars.getSeekBarValue();



        bitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setColor(DARK_PINK);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStrokeWidth(30);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
        // paint = new Paint(Paint.DITHER_FLAG);
        paths.add(path);


        GlobalVars.setBm(bitmap);



        image.setImageBitmap(bitmap);
        image.setOnTouchListener(this);

        /*****/

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        btn_color_one = (Button) findViewById(R.id.btn_color_one);
        btn_color_one.setOnClickListener(this);

        btn_color_two = (Button) findViewById(R.id.btn_color_two);
        btn_color_two.setOnClickListener(this);
        btn_color_three = (Button) findViewById(R.id.btn_color_three);
        btn_color_three.setOnClickListener(this);
        btn_color_four = (Button) findViewById(R.id.btn_color_four);
        btn_color_four.setOnClickListener(this);
        btn_color_five = (Button) findViewById(R.id.btn_color_five);
        btn_color_five.setOnClickListener(this);
        btn_color_six = (Button) findViewById(R.id.btn_color_six);
        btn_color_six.setOnClickListener(this);
        btn_color_seven = (Button) findViewById(R.id.btn_color_seven);
        btn_color_seven.setOnClickListener(this);
        btn_color_eight = (Button) findViewById(R.id.btn_color_eight);
        btn_color_eight.setOnClickListener(this);
        btn_color_nine = (Button) findViewById(R.id.btn_color_nine);
        btn_color_nine.setOnClickListener(this);
        btn_color_ten = (Button) findViewById(R.id.btn_color_ten);
        btn_color_ten.setOnClickListener(this);
        btn_color_elevan = (Button) findViewById(R.id.btn_color_elevan);
        btn_color_elevan.setOnClickListener(this);
        btn_color_twelve = (Button) findViewById(R.id.btn_color_twelve);
        btn_color_twelve.setOnClickListener(this);
        btn_color_thirteen = (Button) findViewById(R.id.btn_color_thirteen);
        btn_color_thirteen.setOnClickListener(this);
        btn_color_fourteen = (Button) findViewById(R.id.btn_color_fourteen);
        btn_color_fourteen.setOnClickListener(this);
        btn_color_fifteen = (Button) findViewById(R.id.btn_color_fifteen);
        btn_color_fifteen.setOnClickListener(this);
        btn_color_sixteen = (Button) findViewById(R.id.btn_color_sixteen);
        btn_color_sixteen.setOnClickListener(this);

        btn_paintbrush = (Button) findViewById(R.id.btn_paintbrush);
        btn_paintbrush.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                initializeMP();
                playsound.start();

                if (seekBar1.getVisibility() == View.INVISIBLE) {
                    seekBar1.setVisibility(View.VISIBLE);
                    seekBar1.setMax(100);
                    seekBar1.setProgress(30);
                    // int currLevel = getPaintAlpha();
                    // seekBar1.setProgress(currLevel);
                } else if (seekBar1.getVisibility() == View.VISIBLE) {
                    seekBar1.setVisibility(View.INVISIBLE);
                }
            }

        });



        seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
                prefs = getApplicationContext().getSharedPreferences(
                        "mySharedPrefsFilename", Context.MODE_PRIVATE);
                // Don't forget to call commit() when changing preferences.
                prefs.edit().putInt("seekBarValue", seekBar.getProgress())
                        .commit();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                GlobalVars.setSeekBarValue(progress);
                paint.setStrokeWidth(progress);
            }
        });

        btn_home = (Button) findViewById(R.id.btn_home);
        btn_home.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                initializeMP();
                playsound.start();

                Intent i = new Intent(Play_Screen.this, Level_Screen.class);
                startActivity(i);
            }
        });

        btn_cam = (Button) findViewById(R.id.btn_cam);
        btn_cam.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                initializeMP();
                playsound.start();

                View view = findViewById(R.id.relative);
                view.setDrawingCacheEnabled(true);
                // Bitmap bitmap2 = view.getDrawingCache();
                final Bitmap bitmap2 = bitmap.copy(Bitmap.Config.ARGB_8888,
                        true);
                final BitmapDrawable bitmapDrawable = new BitmapDrawable(
                        bitmap2);
                image.setBackground(bitmapDrawable);

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View layout = inflater.inflate(R.layout.horizon,
                        (ViewGroup) findViewById(R.id.main_relative_output));
                // View layout = inflater.inflate(R.layout.horizon, (ViewGroup)
                // findViewById(R.id.imageview_output));
                RelativeLayout rl = (RelativeLayout) layout
                        .findViewById(R.id.main_relative_output);
                // ImageView imgview_output =
                // (ImageView)layout.findViewById(R.id.imageview_output);
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        Play_Screen.this).setView(layout);
                rl.setBackground(bitmapDrawable);
                // imgview_output.setBackground(bitmapDrawable);

                // store image
                Button btn_camera_output = (Button) layout
                        .findViewById(R.id.btn_camera_output);
                btn_camera_output.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub



                        if (Environment.MEDIA_MOUNTED.equals(Environment
                                .getExternalStorageState())) {
                            // we check if external storage is available,
                            // otherwise display an error message to the user

                            File sdCard = Environment
                                    .getExternalStorageDirectory();
                            File directory = new File(sdCard.getAbsolutePath()
                                    + "/Kiddy World");
                            directory.mkdirs();

                            String filename = "Painting.jpg";
                            File yourFile = new File(directory, filename);

                            if (directory.canWrite()) {
                                try {
                                    FileOutputStream out = new FileOutputStream(
                                            yourFile);
                                    bitmap2.compress(Bitmap.CompressFormat.PNG,
                                            90, out);

                                    out.flush();
                                    out.close();
                                    Toast.makeText(
                                            getApplicationContext(),
                                            "File exported to /sdcard/Kiddy World/Painting.jpg",
                                            Toast.LENGTH_SHORT).show();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                            }

                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "SD Card not available!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                Button btn_email_output = (Button) layout
                        .findViewById(R.id.btn_email_output);
                btn_email_output.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        File jpegfile = new File("", "Painting.jpg");

                        Bitmap bitmap_output = bitmapDrawable.getBitmap();

                        // Save this bitmap to a file.
                        File cache = getApplicationContext()
                                .getExternalCacheDir();
                        File sharefile = new File(cache, "Painting.jpg");
                        try {
                            FileOutputStream out = new FileOutputStream(
                                    sharefile);
                            bitmap_output.compress(Bitmap.CompressFormat.PNG,
                                    100, out);
                            out.flush();
                            out.close();
                        } catch (IOException e) {
                        }

                        // Now send it out to share
                        Intent share = new Intent(
                                android.content.Intent.ACTION_SEND);
                        share.setType("image/*");
                        share.putExtra(Intent.EXTRA_STREAM,
                                Uri.parse("file://" + sharefile));
                        share.putExtra(Intent.EXTRA_SUBJECT,
                                "Kiddy World VARMORA");
                        share.putExtra(
                                Intent.EXTRA_TEXT,
                                "Kiddy World VARMORA"
                                        + "\n"
                                        + share.putExtra(
                                                Intent.EXTRA_STREAM,
                                                Uri.parse("file://" + sharefile)));
                        try {
                            startActivity(Intent.createChooser(share,
                                    "Share photo"));
                        } catch (Exception e) {
                        }

                    }


                });

                Button btn_facebook_output = (Button) layout
                        .findViewById(R.id.btn_facebook_output);
                btn_facebook_output.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                    }
                });

                AlertDialog alertDialog = builder.create();
                alertDialog.show();

                WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

                lp.copyFrom(alertDialog.getWindow().getAttributes());
                lp.width = 800;
                lp.height = 480;
                lp.x = -170;
                lp.y = 100;
                alertDialog.getWindow().setAttributes(lp);


            }
        });

        btn_cancel = (Button) findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                initializeMP();
                playsound.start();

                bitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
                image.setImageBitmap(bitmap);
            }
        });

        btn_undo = (Button) findViewById(R.id.btn_undo);
        btn_undo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                initializeMP();
                playsound.start();

                onClickUndo();

            }
        });

        btn_redo = (Button) findViewById(R.id.btn_redo);
        btn_redo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                initializeMP();
                playsound.start();


                onClickRedo();
            }
        });

        btn_eraser = (Button) findViewById(R.id.btn_eraser);
        btn_eraser.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                initializeMP();
                playsound.start();



                setErase(true);


            }
        });

        btn_prev = (Button) findViewById(R.id.btn_prev);
        btn_prev.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                initializeMP();
                playsound.start();

                Toast.makeText(getApplicationContext(), "Previous",
                        Toast.LENGTH_SHORT).show();
            }
        });

        btn_next = (Button) findViewById(R.id.btn_next);
        btn_next.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                initializeMP();
                playsound.start();



                Bitmap img_A = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_a);



                Bitmap img_B = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_b);
                // BitmapFactory.Options options = new BitmapFactory.Options();
                // options.inSampleSize = 4;
                // Bitmap img_B = BitmapFactory.decodeFile("R.drawable.img_b",
                // options);
                Bitmap img_C = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_c);
                // Bitmap img_C = BitmapFactory.decodeFile("R.drawable.img_c",
                // options);
                Bitmap img_D = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_d);
                Bitmap img_E = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_e);
                Bitmap img_F = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_f);
                Bitmap img_G = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_g);
                Bitmap img_H = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_h);
                Bitmap img_I = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_i);
                Bitmap img_J = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_j);
                Bitmap img_K = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_k);
                Bitmap img_L = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_l);
                Bitmap img_M = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_m);
                Bitmap img_N = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_n);
                Bitmap img_O = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_o);
                Bitmap img_P = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_p);
                Bitmap img_Q = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_q);
                Bitmap img_R = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_r);
                Bitmap img_S = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_s);
                Bitmap img_T = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_t);
                Bitmap img_U = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_u);
                Bitmap img_V = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_v);
                Bitmap img_W = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_w);
                Bitmap img_X = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_x);
                Bitmap img_Y = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_y);
                Bitmap img_Z = BitmapFactory.decodeResource(getResources(),
                        R.drawable.img_z);


                if (bitmapequals(bitmap, img_A)) {

                    bitmap = img_B.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);
                } else if (bitmapequals(bitmap, img_B)) {

                    bitmap = img_C.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);
                     100));
                } else if (bitmapequals(bitmap, img_C)) {

                    bitmap = img_D.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);
                    00, 100));
                } else if (bitmapequals(bitmap, img_D)) {

                    bitmap = img_E.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);
                     100, 100));
                } else if (bitmapequals(bitmap, img_E)) {

                    bitmap = img_F.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);
                    / 100, 100));
                } else if (bitmapequals(bitmap, img_F)) {

                    bitmap = img_G.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_G)) {

                    bitmap = img_H.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);
                } else if (bitmapequals(bitmap, img_H)) {

                    bitmap = img_I.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_I)) {

                    bitmap = img_J.copy(Bitmap.Config.ARGB_8888, true);

                } else if (bitmapequals(bitmap, img_J)) {

                    bitmap = img_K.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_K)) {

                    bitmap = img_L.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_L)) {

                    bitmap = img_M.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_M)) {

                    bitmap = img_N.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_N)) {

                    bitmap = img_O.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_O)) {

                    bitmap = img_P.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_P)) {

                    bitmap = img_Q.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_Q)) {

                    bitmap = img_R.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_R)) {

                    bitmap = img_S.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_S)) {

                    bitmap = img_T.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_T)) {

                    bitmap = img_U.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_U)) {

                    bitmap = img_V.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_V)) {

                    bitmap = img_W.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_W)) {

                    bitmap = img_X.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_X)) {

                    bitmap = img_Y.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);

                } else if (bitmapequals(bitmap, img_Y)) {

                    bitmap = img_Z.copy(Bitmap.Config.ARGB_8888, true);
                    image.setImageBitmap(bitmap);
                    btn_next.setVisibility(View.INVISIBLE);

                } else if (bitmapequals(bitmap, img_Z)) {



                }




            }
        });



    }



    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        // int action = event.getAction();
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            path.moveTo(eventX, eventY);
            paths.add(path);
            break;

        case MotionEvent.ACTION_MOVE:
            path.lineTo(eventX, eventY);
            canvas.drawPath(path, paint);
            paths.add(path);
            break;

        case MotionEvent.ACTION_UP:

            upx = event.getX();
            upy = event.getY();
            // canvas.drawLine(downx, downy, upx, upy, paint);

            path.lineTo(eventX, eventY);
            canvas.drawPath(path, paint);
            paths.add(path);
            path.reset();

            break;

        case MotionEvent.ACTION_CANCEL:
            break;
        default:

            break;
        }
        image.invalidate();
        v.invalidate();

        return true;
    }

    // set erase true or false
    @SuppressWarnings("deprecation")
    public void setErase(boolean isErase) {
        erase = isErase;
        if (erase) {
             paint.setXfermode(null);



            paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.WHITE);
            paint.setXfermode(new AvoidXfermode(Color.WHITE, 215,
                    AvoidXfermode.Mode.TARGET));
            paint.setMaskFilter(new BlurMaskFilter(15, Blur.SOLID));

        } else {
            paint.setXfermode(null);
        }
    }


    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        while (btn_cancel.isClickable()) {
            canvas = new Canvas(bitmap);
            paint = new Paint();
            paint.setColor(DARK_PINK);
            paint.setAntiAlias(true);
            paint.setDither(true);

            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);

            path = new Path();
            image.setImageBitmap(bitmap);
            break;
        }

        switch (v.getId()) {

        case R.id.btn_color_one: {
            // do something for button 1 click
             initializeMP();
             playsound.start();

             setupDrawing();
             paint.setColor(DARK_PINK);

            break;

        }
        case R.id.btn_color_two: {
            // do something for button 1 click

             initializeMP();
             playsound.start();

             setupDrawing();
             paint.setColor(LIGHT_YELLOW);

            break;
        }
        case R.id.btn_color_three: {
            // do something for button 1 click

             initializeMP();
             playsound.start();

            setupDrawing();
            paint.setColor(DARK_MARUN);


            break;
        }
        case R.id.btn_color_four: {
            // do something for button 1 click

             initializeMP();
             playsound.start();


             setupDrawing();

            break;
        }
        case R.id.btn_color_five: {
            // do something for button 1 click

             initializeMP();
             playsound.start();

             setupDrawing();
             paint.setColor(RED);

            break;
        }

        }
    }

    public void onClickUndo() {
        if (paths.size() > 0) {
            undonePaths.add(paths.remove(paths.size() - 1));
            Toast.makeText(getApplicationContext(),
                    String.valueOf(paths.size()), 2).show();
            image.refreshDrawableState();
            image.invalidate();
        } else {
            // toast the user
            Toast.makeText(getApplicationContext(), "Not Work Undo",
                    Toast.LENGTH_SHORT).show();
        }
    }

    public void onClickRedo() {
        if (undonePaths.size() > 0) {
            paths.add(undonePaths.remove(undonePaths.size() - 1));
        } else {
            // toast the user
            Toast.makeText(getApplicationContext(), "Not Work Redo",
                    Toast.LENGTH_SHORT).show();
        }

    }



    private void initializeMP() {
        // TODO Auto-generated method stub
        playsound = MediaPlayer.create(this, R.raw.playsound);
    }


    public boolean bitmapequals(Bitmap bitmap1, Bitmap bitmap2) {

        ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight()
                * bitmap1.getRowBytes());
        bitmap1.copyPixelsToBuffer(buffer1);

        ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight()
                * bitmap2.getRowBytes());
//      bitmap2.copyPixelsFromBuffer(buffer1);
        bitmap2.copyPixelsToBuffer(buffer2);

        return Arrays.equals(buffer1.array(), buffer2.array());
    }


    public void setupDrawing() {
        initializeMP();
        playsound.start();

        canvas = new Canvas(bitmap);
        paint.setAntiAlias(true);
        paint.setDither(false);
        paint.setStrokeWidth(30);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
        // paint.setXfermode (new PorterDuffXfermode (Mode.SRC_IN));
    }




    boolean imagesAreEqual(Bitmap i1, Bitmap i2) {
        if (i1.getHeight() != i2.getHeight())
            return false;
        if (i1.getWidth() != i2.getWidth())
            return false;

        for (int y = 0; y < i1.getHeight(); ++y)
            for (int x = 0; x < i1.getWidth(); ++x)
                if (i1.getPixel(x, y) != i2.getPixel(x, y))
                    return false;

        return true;
    }



    }

3 个答案:

答案 0 :(得分:1)

如果您希望图像中的多个图层在每个图层上具有不同的颜色,则可以使用A LayerDrawableLayerDrawable允许您创建具有不同图层的Drawable对象。您可以根据需要定位图层。

示例:

Drawable largeIcon = getResources().getDrawable(R.drawable.app_icon);

ShapeDrawable bg = new ShapeDrawable();
bg.getPaint().setColor(Color.RED);

Drawable[] layers = {bg, largeIcon};

LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 35, 35, 35, 35);  // relative positioning of next layer 

return layerDrawable

答案 1 :(得分:1)

在ACTION_UP上你做了一件奇怪的事,这是你的代码:

        path.lineTo(eventX, eventY);
        canvas.drawPath(path, paint);
        paths.add(path);
        path.reset();

最后路径很清楚。下次到达那里,你添加一个lineTo,并绘制它,这将是一个空路径。您添加路径的路径,并清除路径。这是我看到的唯一一个画布绘图代码,所以我甚至不知道你的问题在哪里

答案 2 :(得分:1)

您遇到的问题是使用PorterDuff.Mode绘制所有路径。你真正想做的是在没有任何PorterDuff.Mode的情况下绘制所有路径,然后进行最终渲染并将其应用于具有PorterDuff.Mode的A形状。如果有更多的时间,我应该能够为你写一个例子。您的代码还有许多其他问题。

解决方案:https://github.com/slightfoot/android-painter-example