1

这是我可以在视图上绘制的代码,但是当我为视图设置背景时,我无法在背景图像上绘制,请帮帮我。提前致谢

  public class FingerPaint extends GraphicsActivity
  implements MyColorPickerDialog.OnColorChangedListener {    


   ListView dialog_ListView;
   String[] listContent = {
        "Stroke Size 1", "Stroke Size 2", "Stroke Size 3", "Stroke Size 4", 
        "Stroke Size 5","Stroke Size 10","Stroke Size 20","Stroke Size 30"
};
String[] effectContent = {
        "Normal", "Blur", "Emboss", "SrcATop" };
private static  int FONT_SIZE=5;
private static int COLOR_CODE=Color.BLACK;
public int mPos;
private Bitmap  mBitmap;
MyView mv;

ImageShowActivity imageShowActivity =new ImageShowActivity();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mv= new MyView(this);
    mv.setDrawingCacheEnabled(true);
    //mv.setBackgroundResource(R.drawable.image1_layer1);
    setContentView(mv);
    initialiazation();
    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 },
            0.4f, 6, 3.5f);

    mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);// TODO Auto-generated method stub
    loadImageToEddit();
}

private void initialiazation() {
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(COLOR_CODE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(FONT_SIZE);



}
private void loadImageToEddit() {

    Bundle bundle=getIntent().getExtras();
    if(bundle!=null){
        int  pos=bundle.getInt("image_path");
        if(pos!=0){
            // Bitmap bitmap =BitmapFactory.decodeFile(ImageShowActivity.imagePathList.get(path));
            //   mv=new MyView(this,bitmap);
            //Drawable d = new BitmapDrawable(getResources(),bitmap);
            //mv.setBackground(d);
            mPos=pos;

        }
    }
}

private Paint       mPaint;
private MaskFilter  mEmboss;
private MaskFilter  mBlur;

public void colorChanged(int color) {
    mPaint.setColor(color);
    COLOR_CODE=color;
}

private void fontChanged(Float size){
    mPaint.setStrokeWidth(size);
}
private void showEffectDialog(){
    final Dialog    dialog = new Dialog(FingerPaint.this);
    dialog.setContentView(R.layout.font_dialog);
    dialog.setCancelable(true);
    dialog.setTitle("Font");
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.width=200; params.height=250;
    params.gravity =  Gravity.CENTER_HORIZONTAL;       
    dialog.getWindow().setAttributes(params);
    ArrayAdapter<String> ad =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,effectContent);

    ListView list = (ListView) dialog.findViewById(R.id.dialoglist);
    list.setAdapter(ad);
    dialog.show();
    list.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {
            switch (pos) {
            case 0:
                mPaint.setMaskFilter(null);
                initialiazation();

                break;
            case 1:
                mPaint.setMaskFilter(null);
                //initialiazation();
                mPaint.setStrokeWidth(FONT_SIZE);
                if (mPaint.getMaskFilter() != mBlur) {
                    mPaint.setMaskFilter(mBlur);
                } else {
                    mPaint.setMaskFilter(null);
                }

                break;
            case 2:
                mPaint.setMaskFilter(null);
                if (mPaint.getMaskFilter() != mEmboss) {
                    mPaint.setMaskFilter(mEmboss);
                } else {
                    mPaint.setMaskFilter(null);
                }
                break;
            case 3:
                mPaint.setMaskFilter(null);
                mPaint.setXfermode(new PorterDuffXfermode(
                        PorterDuff.Mode.SRC_ATOP));
                mPaint.setAlpha(0x80);
                break;

            default:
                break;
            }
            dialog.dismiss();

        }
    });


}
private void showDialog(){
    final Dialog    dialog = new Dialog(FingerPaint.this);
    dialog.setContentView(R.layout.font_dialog);
    dialog.setCancelable(true);
    dialog.setTitle("Font");
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.width=200; params.height=250;
    params.gravity =  Gravity.CENTER_HORIZONTAL;       
    dialog.getWindow().setAttributes(params);
    ArrayAdapter<String> ad =new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listContent);

    ListView list = (ListView) dialog.findViewById(R.id.dialoglist);
    list.setAdapter(ad);
    dialog.show();
    list.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                long arg3) {
            switch (pos) {
            case 0:
                FONT_SIZE=1;
                fontChanged(1f);
                break;
            case 1:
                FONT_SIZE=2;
                fontChanged(2f);
                break;
            case 2:
                FONT_SIZE=3;
                fontChanged(3f);
                break;
            case 3:
                FONT_SIZE=4;
                fontChanged(4f);
                break;
            case 4:
                FONT_SIZE=5;
                fontChanged(5f);
                break;
            case 5:
                FONT_SIZE=10;
                fontChanged(10f);
                break;
            case 6:
                FONT_SIZE=20;
                fontChanged(20f);
                break;
            case 7:
                FONT_SIZE=30;
                fontChanged(30f);
                break;
            default:
                break;
            }
            dialog.dismiss();

        }
    });


}


public class MyView extends View {

    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;

    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;

    public MyView(Context c) {
        super(c);
        //            DisplayMetrics dm = new DisplayMetrics();
        //            getWindowManager().getDefaultDisplay().getMetrics(dm);
        //            int w = dm.widthPixels; 
        //            int h=dm.heightPixels;
        //            mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        // mBitmap.eraseColor(c)

        //            mCanvas = new Canvas(mBitmap);
        //            mCanvas.drawColor(0xFFAAAAAA);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        // mPaint.setStrokeWidth(FONT_SIZE);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        if(mPos!=0){
            Bitmap bitmap =BitmapFactory.decodeFile(ImageShowActivity.imagePathList.get(mPos));
            mBitmap=bitmap;
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            //canvas.drawPath(mPath, mPaint);
            Log.d("onDraw", "if called");


            //invalidate();
        }else{
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            Log.d("onDraw", "else called");

        }

        canvas.drawPath(mPath, mPaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touch_start(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();
            break;
        }
        return true;
    }
}

private static final int COLOR_MENU_ID = Menu.FIRST;
private static final int FONT_ID = Menu.FIRST + 1;
private static final int BLUR_MENU_ID = Menu.FIRST + 2;
private static final int ERASE_MENU_ID = Menu.FIRST + 3;
private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
    menu.add(0, FONT_ID, 0, "Font Size").setShortcut('4', 's');
    menu.add(0, BLUR_MENU_ID, 0, "Effects").setShortcut('5', 'z');
    menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
    menu.add(0, SRCATOP_MENU_ID, 0, "Save").setShortcut('5', 'z');

    /****   Is this the mechanism to extend with filter effects?
    Intent intent = new Intent(null, getIntent().getData());
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    menu.addIntentOptions(
                          Menu.ALTERNATIVE, 0,
                          new ComponentName(this, NotesList.class),
                          null, intent, 0, null);
     *****/
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    mPaint.setXfermode(null);
    mPaint.setAlpha(0xFF);

    switch (item.getItemId()) {
    case COLOR_MENU_ID:
        //  initialiazation();

        new MyColorPickerDialog(this,this, Color.RED, false).show();
        return true;
    case FONT_ID:

        showDialog();
        //emboss
        //              
        return true;
    case BLUR_MENU_ID:
        showEffectDialog();
        //                
        return true;
    case ERASE_MENU_ID:
        //initialiazation();
        mPaint.setAlpha(0xFF);//transperent color
        mPaint.setMaskFilter(null);

        mPaint.setXfermode(new PorterDuffXfermode(
                PorterDuff.Mode.CLEAR));

        return true;
    case SRCATOP_MENU_ID:
        //                mPaint.setXfermode(new PorterDuffXfermode(
        //                                                    PorterDuff.Mode.SRC_ATOP));
        //                mPaint.setAlpha(0x80);

        saveImageDialog2();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
//src fingerpaint.java

private void saveImageDialog2() {
    AlertDialog.Builder editalert = new AlertDialog.Builder(FingerPaint.this);
    editalert.setTitle("Please Enter the name with which you want to Save");
    final EditText input = new EditText(FingerPaint.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    input.setLayoutParams(lp);
    editalert.setView(input);
    editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            String name= input.getText().toString();
            Bitmap bitmap = mv.getDrawingCache();
            File    directory= new File(Environment.getExternalStorageDirectory(),"Hand Brush/");

            File    myPath=new File(directory,name);
            File file = new File(myPath+".png");           
            try 
            {
                if(!file.exists())
                {
                    file.createNewFile();
                }
                FileOutputStream ostream = new FileOutputStream(file);
                bitmap.compress(CompressFormat.PNG, 10, ostream);
                //System.out.println("saving......................................................"+path);
                ostream.close();
                mv.invalidate();                            
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }finally
            {

                mv.setDrawingCacheEnabled(false);                           
            }
        }
    });

    editalert.show();       
}

}

4

1 回答 1

0

在 onDraw() 中删除它

canvas.drawColor(Color.White);

mv= new MyView(this);
mv.setDrawingCacheEnabled(true);
mv.setBackgroundResource(R.drawable.afor);
setContentView(mv);

抽奖时

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
   canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
   canvas.drawPath(mPath, mPaint);
  }

快照

在此处输入图像描述

于 2013-04-05T10:41:40.073 回答