Android : how to create a button over Bitmap dynamically or OVERLAYING button?

I am trying to create a BUTTON over BITMAP or OVERLAYING buttons on the view. I am creating digital signature here. after signature i need to save for that i need a button. please anyone help me out. Its very urgent. Thank you

Here my code

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

 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);
}


}

I am able to create image & cant start activity. Please let me know how to solve this issue.


Here I got the way to create dynamically buttons on 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();
                } 
            }  
        });     
} 

}

Thanks guys for helping..


if you place your ImageView and Button inside an RelativeLayout they will overlap eachother. You can play with positioning as needed.

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

<ImageView 
android:....
android:....
/>
<Button 
android:....
android:....
/>
</RelativeLayout>
链接地址: http://www.djcxy.com/p/58568.html

上一篇: 在javascript中实现一个squarified treemap

下一篇: Android:如何动态地或通过OVERLAYING按钮来创建一个超过位图的按钮?