2 hcg 21 hcg_21 于 2012.11.26 13:17 提问

在Bitmap或者OVERLAYING按钮上如何动态的创建一个按钮

我想在视图的 BITMAP或者OVERLAYING按钮上面创建一个按钮。我创建了一个数字签名,签名后,我需要点击一个按钮来保存。我使用的如下代码

public class FingerPaint extends GraphicsActivity
   implements OnClickListener {  
private TextView pauseButton;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MyView(this));

    Capture_SignatureActivity.writeLog("In FingerPaint class");
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(0xFF003F87);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(1); 

} 
private Paint mPaint;

public void colorChanged(int color) {
    Capture_SignatureActivity.writeLog("In color changed");
    mPaint.setColor(color);
}

public class MyView extends View{
    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;

    private Bitmap  mBitmap;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;
    private Bitmap overlayDefault;
    private Bitmap overlay;
    private Canvas c2;
    private Paint pTouch;
    private float X =  -100;
    private float Y =  -100;



    public MyView(Context c) {
        super(c);
        Capture_SignatureActivity.writeLog("In MyVIEW");
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);  


        mBitmap =      BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        overlayDefault = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        overlay = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher).copy(Config.ARGB_8888, true);  
        c2 = new Canvas(overlay);         



        pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);         
        pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT)); 
        pTouch.setColor(Color.TRANSPARENT);
        pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
        pTouch.setMaskFilter(startActivity(FingerPaint.this, Capture_SignatureActivity.class));



    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        System.out.println("w-->"+w+"-->"+"h--"+h+"-->"+"oldw-->"+oldh+"oldw-->"+oldw);
        Capture_SignatureActivity.writeLog("In  onSizeChanged");
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);            
        mCanvas = new Canvas(mBitmap);       
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Capture_SignatureActivity.writeLog("In onDraw");
        canvas.drawColor(0xFFFFFFFF);       

        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);

        c2.drawBitmap(overlayDefault, 0, 0, null); //exclude this line to show all as you draw
        c2.drawCircle(X , Y , 80, pTouch);
        //draw the overlay over the background  
        canvas.drawBitmap(overlay, 0, 0, null);

    }

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

    private void touch_start(float x, float y) {
        Capture_SignatureActivity.writeLog("In  touch_start");
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
        System.out.println("---- " +mX);
    }
    private void touch_move(float x, float y) {
        Capture_SignatureActivity.writeLog("In  touch_Move");
        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() {
        Capture_SignatureActivity.writeLog("In 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) {
        Capture_SignatureActivity.writeLog("In  touch_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;
    }

    public void onClick(View v) {
        //In myView

        startActivity(new Intent(FingerPaint.this, Capture_SignatureActivity.class));


    }
}

public void onClick(View v) {
    //onCreate

}

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

    menu.add(0, 1, 0 , "Clear Screen");
    menu.add(0, 2, 0 , "Exit Screen");

    return true;
} 

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
    switch(item.getItemId()) 
    {
    case 1: 
        startActivity(new Intent(this, FingerPaint.class));
        break; 
    case 2: 
        startActivity(new Intent(this, Capture_SignatureActivity.class));
        break; 

    }
    return super.onMenuItemSelected(featureId, item);
}


}

创建了图像,但是不能开启activity。怎么解决这个问题呢?

2个回答

Baby_Bonnie
Baby_Bonnie   2012.11.26 16:38
已采纳

在BITMAP或者CANVAS上动态的创建按钮的方法:

public class MainAct extends GrapActivity implements OnClickListener {


private Button saveButton;
private Button clearButton;

// onCreate Activity
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);          

    // content view 
    signature = new MyView(this); 

    // Dynamically created button on bitmap & canvas

    RelativeLayout myLayout = new RelativeLayout(this);        
    myLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    // For Save Button
    saveButton = new Button(this); 
    saveButton.setText("Save"); 
    saveButton.setOnClickListener(this); 

    // For Clear Button
    clearButton = new Button(this); 
    clearButton.setText("Clear"); 
    clearButton.setOnClickListener(this); 

    myLayout.addView(signature); 
    myLayout.addView(saveButton); 
    myLayout.addView(clearButton); 

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT); 
    //Alignments
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    saveButton.setLayoutParams(params); 

    //Alignments
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT); 
    params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    clearButton.setLayoutParams(params2); 

    saveButton.bringToFront(); 
    clearButton.bringToFront(); 
    this.setContentView(myLayout); 
    new Thread(new RefreshRunner()).start(); 

    // onclick listner for CLEAR button
    clearButton.setOnClickListener(new OnClickListener() {          
        public void onClick(View v) {
            //Activity for Clearing the Screen
            startActivity(new Intent(ThisAct.this, ThisAct.class));
            finish();
        }
    });

 // onclick listner for SAVE button
        saveButton.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) { 
                //capture the image  
                try {                    
                    saveAsJpg(mBitmap);     
                    startActivity(new Intent(ThisAct.this, MainActivity.class));
                    finish();
                } catch (IOException e) {                   
                    e.printStackTrace();
                } 
            }  
        });     
} 

}
LiDaming_and
LiDaming_and   2012.11.26 17:15

如果你在RelativeLayout同时添加ImageView和Button。它们会重叠在一起。你可以根据需要设置下定位。

<RelativeLayout 
android:layout_width="wrap_content"
android:layout_height="match_parent" >

<ImageView 
android:....
android:....
/>
<Button 
android:....
android:....
/>
</RelativeLayout>
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!