I have this signature view, which allows me to mostly draw on the phone screen. Now I need to add this view as a background or use it as a widget in the xml layout, but I have no idea how to do this. So please, can someone help me. This is the signature view class that I have:
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class SignatureView extends View { private static final float STROKE_WIDTH = 5f; private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2; private Paint paint = new Paint(); private Path path = new Path(); private float lastTouchX; private float lastTouchY; private final RectF dirtyRect = new RectF(); public SignatureView(Context context, AttributeSet attrs, int background) { super(context, attrs); setBackgroundResource(background); paint.setAntiAlias(true); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeWidth(STROKE_WIDTH); } public void setColor(int color){ paint.setColor(color); } public void clear() { path.reset();
and my main class:
public class Draw extends Activity { DrawView drawView; SignatureView signature; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set full screen view getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); signature = new SignatureView(this, null,R.drawable.back); setContentView(signature); signature.requestFocus(); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.my_options_menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.clear: signature.clear(); return true; case R.id.red: signature.setColor(Color.RED); return true; case R.id.blue: signature.setColor(Color.BLUE); return true; case R.id.yellow: signature.setColor(Color.YELLOW); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onBackPressed() { this.finish(); super.onBackPressed(); } }
source share