I want to create a window where the user can go out of the borders of the image but the path continues.
I have this code for my Activity
public class PaintActivity extends Activity {
PaintView maskView;
ImageView imagen;
Bitmap bitmap;
Bitmap mask;
int bitmapWidth, bitmapHeight;
public int[] getBitmapMinCoords() {
float[] minCoordF = {0, 0};
Matrix matriz = imagen.getImageMatrix();
matriz.mapPoints(minCoordF);
int[] minCoords = new int[2];
minCoords[0] = Math.round(minCoordF[0]);
minCoords[1] = Math.round(minCoordF[1]);
return minCoords;
}
public int[] getBitmapMaxCoords() {
float[] maxCoordF = {bitmapWidth, bitmapHeight};
Matrix matriz = imagen.getImageMatrix();
matriz.mapPoints(maxCoordF);
int[] maxCoords = new int[2];
maxCoords[0] = Math.round(maxCoordF[0]);
maxCoords[1] = Math.round(maxCoordF[1]);
return maxCoords;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.paint);
imagen = (ImageView) findViewById(R.id.image_paint);
maskView = (PaintView) findViewById(R.id.mask_paint);
String imagePath = "/sdcard/images/NewOrleans.gif";
bitmap = BitmapFactory.decodeFile(imagePath);
imagen.setImageBitmap(bitmap);
bitmapWidth = bitmap.getWidth();
bitmapHeight = bitmap.getHeight();
ViewTreeObserver vto = imagen.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
Matrix matrizTransformacion = imagen.getImageMatrix();
float[] values = new float[9];
if (matrizTransformacion != null) {
matrizTransformacion.getValues(values);
Log.i(PaintActivity.class.toString() + ".globalListener()", "La matriz tiene valores: " + matrizTransformacion.toString() );
}
int[] minC = getBitmapMinCoords();
int[] maxC = getBitmapMaxCoords();
Log.i("Listener MinCOORDS (0,0)", minC[0] + ", " + minC[1]);
Log.i("Listener MaxCOORDS (" + bitmapWidth + "," + bitmapHeight + ")", maxC[0] + ", " + maxC[1]);
Bitmap mapaBits = Bitmap.createBitmap( bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mapaBits);
maskView.layout(minC[0], minC[1], maxC[0], maxC[1]);
maskView.draw(canvas);
maskView.refreshDrawableState();
imagen.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
Bitmap getMaskBitmap(View view) {
Matrix matrizTransformacion = imagen.getImageMatrix();
float[] values = new float[9];
if (matrizTransformacion != null) {
matrizTransformacion.getValues(values);
Log.i(PaintActivity.class.toString() + ".onStart()", "La matriz tiene valores: " + matrizTransformacion.toString() );
}
Bitmap mapaBits = Bitmap.createBitmap( Math.round(bitmapWidth*values[0]), Math.round(bitmapHeight*values[4]), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mapaBits);
int[] minCoords = getBitmapMinCoords();
int[] maxCoords = getBitmapMaxCoords();
view.layout(minCoords[0], minCoords[1], maxCoords[0], maxCoords[1]);
view.draw(canvas);
return mapaBits;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.inpainting_color_selection_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.inpainting_color_accept_opt:
ProgressDialog pd = ProgressDialog.show(this, "", "Guardando imagen");
Bitmap mascara = getMaskBitmap(maskView);
// GUARDANDO BITMAP EN HDD
try {
String filename = "/sdcard/PhotoRestore/mask.png";
File file = new File(filename);
FileOutputStream out = new FileOutputStream(file);
mascara.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
}
pd.dismiss();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
This is the code for my PaintView
public class PaintView extends View {
float previousX = -1;
float previousY = -1;
float currentX = -1;
float currentY = -1;
PaintActivity activity;
Path path;
Paint paintLine = new Paint();
public PaintView(Context context) {
super(context);
init();
}
public PaintView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PaintView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
path = new Path();
activity = (PaintActivity) getContext();
paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setStrokeWidth(10);
paintLine.setColor(Color.GREEN);
paintLine.setAlpha(150);
}
public void onDraw(Canvas canvas) {
canvas.drawPath(path, paintLine);
}
#Override
public boolean onTouchEvent(final MotionEvent event) {
currentX = event.getX();
currentY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(currentX, currentY);
break;
case MotionEvent.ACTION_MOVE:
path.quadTo(previousX, previousY, currentX, currentY);
break;
case MotionEvent.ACTION_UP:
path.quadTo(previousX, previousY, currentX, currentY);
break;
}
previousX = currentX;
previousY = currentY;
postInvalidate();
return true;
}
}
And this is my XML view
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/image_paint"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/hola" />
<com.paint.PaintView
android:id="#+id/mask_paint"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
When I push the button I added in the menu I save the image inside memory and the screen continues being there but now i've got a border. Far away this border the paths continues drawing but it's occluded under a black surface and paths are not cutted, that's I want to occur at the beginning of the activity.
I tried to do the same in the 'onCreate()' method of the activity (inside the global layout listener) but it don't runs.
Someone could help me?
First of all, lost of thanks!
I found the way to solve my problem.
Where I need to put the view.layout() is inside the onDraw() method of the view that I want to be layout resized.
Related
I am done creating different brush effect in for canvas. When i change the brush then previously drawn paint is change to that brush effect. So what i do for that? If any guidance then please provide me.
Below is my DrawingView class.
public class DrawingView extends View
{
private final Paint mPaintSrcIn = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
private final Paint mPaintDstIn = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
public Paint mPaintColor = new Paint(Paint.ANTI_ALIAS_FLAG);
public Paint mPaintColor1 = new Paint(Paint.ANTI_ALIAS_FLAG);
// public final Paint mPaintColor2 = new Paint(Paint.ANTI_ALIAS_FLAG);
// public final Paint mPaintColor3 = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint mPaintEraser = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Matrix mMatrix = new Matrix();
private final Canvas mLayerCanvas = new Canvas();
private Bitmap mInnerShape;
private Bitmap mOuterShape;
private Bitmap mLayerBitmap;
private Color mInnerColor,mOuterColor;
PlayActivity playActivity = new PlayActivity();
private int mFlag;
private int paintAlpha = 255;
private int paintColor = 0xFFFF0000;
private ArrayList<DrawOp> mDrawOps = new ArrayList<DrawOp>();
private ArrayList<DrawOp> mDrawOps1 = new ArrayList<DrawOp>();
private DrawOp mCurrentOp = new DrawOp();
private DrawOp mPreviousOp = new DrawOp();
private ArrayList<DrawOp> mUndoneOps = new ArrayList<DrawOp>();
public DrawingView(Context context)
{
this(context, null, 0);
}
public DrawingView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public DrawingView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
mPaintSrcIn.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
mPaintDstIn.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mPaintColor.setStyle(Paint.Style.STROKE);
mPaintColor.setStrokeJoin(Paint.Join.ROUND);
mPaintColor.setStrokeCap(Paint.Cap.ROUND);
mPaintEraser.set(mPaintColor);
mPaintEraser.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mPaintEraser.setMaskFilter(new BlurMaskFilter(getResources()
.getDisplayMetrics().density * 4, BlurMaskFilter.Blur.NORMAL));
}
public void setShape(int inner, int outer)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ALPHA_8;
setShape(BitmapFactory.decodeResource(getResources(), inner, options),
BitmapFactory.decodeResource(getResources(), outer, options));
}
public void setShape(Bitmap inner, Bitmap outer)
{
mInnerShape = inner;
mOuterShape = outer;
requestLayout();
invalidate();
}
public void setDrawingColor(int color)
{
mCurrentOp.reset();
mCurrentOp.type = DrawOp.Type.PAINT;
mCurrentOp.color = color;
mPreviousOp.reset();
mPreviousOp.type = DrawOp.Type.PAINT;
mPreviousOp.color = color;
}
public void setDrawingStroke(int stroke)
{
mCurrentOp.reset();
mCurrentOp.type = DrawOp.Type.PAINT;
mCurrentOp.stroke = stroke;
}
public void enableEraser()
{
mCurrentOp.reset();
mCurrentOp.type = DrawOp.Type.ERASE;
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
mLayerBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mLayerCanvas.setBitmap(mLayerBitmap);
if(mOuterShape != null){
int dx = (w - mOuterShape.getWidth()) / 2;
int dy = (h - mOuterShape.getHeight()) / 2;
mMatrix.setTranslate(dx, dy);
}
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if(isInEditMode()){
return;
}
// NOTE: Without extra bitmap or layer.. but HW Acceleration does not support setMaskFilter which means
// eraser has strong edges whilst drawing.
// #see http://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported
System.out.println("Flag before if "+mFlag);
if(mFlag==0){
System.out.println("Flag = 0 "+mFlag);
// Clear software canvas
// mPaintColor1 = mPaintColor;
mPaintColor.setStyle(Paint.Style.STROKE);
mPaintColor.setStrokeJoin(Paint.Join.ROUND);
mPaintColor.setStrokeCap(Paint.Cap.ROUND);
mPaintColor.setMaskFilter(null);
mLayerCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
// Draw picture from ops
for (DrawOp op : mDrawOps) {
drawOp(mLayerCanvas, op);
}
drawOp(mLayerCanvas, mCurrentOp);
System.out.println("Drawing Flag : " + mFlag);
// Mask the drawing to the inner surface area of the shape
mLayerCanvas.drawBitmap(mInnerShape, mMatrix, mPaintDstIn);
// Draw orignal shape to view
canvas.drawBitmap(mOuterShape, mMatrix, null);
// Draw masked image to view
canvas.drawBitmap(mLayerBitmap, 0, 0, null);
}else if(mFlag==1){
mPaintColor.setStyle(Paint.Style.STROKE);
mPaintColor.setStrokeJoin(Paint.Join.ROUND);
mPaintColor.setStrokeCap(Paint.Cap.ROUND);
mPaintColor.setMaskFilter(null);
// Clear software canvas
mLayerCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
// Draw picture from ops
for(DrawOp op : mDrawOps){
drawOp(mLayerCanvas, op);
}
drawOp(mLayerCanvas, mCurrentOp);
// Mask the drawing to the inner surface area of the shape
mLayerCanvas.drawBitmap(mInnerShape, mMatrix, mPaintDstIn);
// Draw orignal shape to view
canvas.drawBitmap(mOuterShape, mMatrix, null);
// Draw masked image to view
canvas.drawBitmap(mLayerBitmap, 0, 0, null);
canvas.save();
}
else if(mFlag==2){
System.out.println("Flag = 2 "+mFlag);
// mPaintColor1 = mPaintColor;
/* Blur Effect for creating more effect then change following properties
BlurMaskFilter.Blur.INNER,BlurMaskFilter.Blur.NORMAL,BlurMaskFilter.Blur.OUTER
BlurMaskFilter.Blur.SOLID*/
// Code Here
BlurMaskFilter mBlur = new BlurMaskFilter(15, BlurMaskFilter.Blur.NORMAL);
mPaintColor.setMaskFilter(mBlur);
// Clear software canvas
mLayerCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
// Draw picture from ops
for (DrawOp op : mDrawOps) {
drawOp(mLayerCanvas, op);
}
drawOp(mLayerCanvas, mCurrentOp);
// Mask the drawing to the inner surface area of the shape
mLayerCanvas.drawBitmap(mInnerShape, mMatrix, mPaintDstIn); /*mPaintDstIn*/
// Draw orignal shape to view
canvas.drawBitmap(mOuterShape, mMatrix, null);
// Draw masked image to view
canvas.drawBitmap(mLayerBitmap, 0, 0, null);
canvas.save();
}
else if(mFlag==3){
mPaintColor.setStyle(Paint.Style.STROKE);
mPaintColor.setStrokeJoin(Paint.Join.ROUND);
mPaintColor.setStrokeCap(Paint.Cap.ROUND);
mPaintColor.setMaskFilter(null);
mPaintColor.setMaskFilter(new EmbossMaskFilter(new float[] { 1, 1, 1 },0.4f, 10, 8.2f));
// Clear software canvas
mLayerCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
// Draw picture from ops
for (DrawOp op : mDrawOps) {
drawOp(mLayerCanvas, op);
}
drawOp(mLayerCanvas, mCurrentOp);
// Mask the drawing to the inner surface area of the shape
mLayerCanvas.drawBitmap(mInnerShape, mMatrix, mPaintDstIn);
// Draw orignal shape to view
canvas.drawBitmap(mOuterShape, mMatrix, null);
// Draw masked image to view
canvas.drawBitmap(mLayerBitmap, 0, 0, null);
canvas.save();
}
}
private void drawOp(Canvas canvas, DrawOp op)
{
if(op.path.isEmpty()){
return;
}
final Paint paint;
if(op.type == DrawOp.Type.PAINT){
paint = mPaintColor;
paint.setColor(op.color);
paint.setStrokeWidth(op.stroke);
}else{
paint = mPaintEraser;
paint.setStrokeWidth(op.stroke);
}
mLayerCanvas.drawPath(op.path, paint);
}
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(MotionEvent event)
{
final float x = event.getX();
final float y = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
mUndoneOps.clear();
mCurrentOp.path.moveTo(x, y);
break;
case MotionEvent.ACTION_MOVE:
for(int i = 0; i < event.getHistorySize(); i++){
mCurrentOp.path.lineTo(event.getHistoricalX(i), event.getHistoricalY(i));
}
mCurrentOp.path.lineTo(x, y);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
mCurrentOp.path.lineTo(x, y);
mDrawOps.add(new DrawOp(mCurrentOp));
mCurrentOp.path.reset();
break;
}
invalidate();
return true;
}
private static class DrawOp
{
public final Path path = new Path();
public Type type;
public int color;
public int stroke;
public DrawOp()
{
//
}
public void reset()
{
this.path.reset();
}
public DrawOp(DrawOp op)
{
this.path.set(op.path);
this.type = op.type;
this.color = op.color;
this.stroke = op.stroke;
}
public static enum Type
{
PAINT, ERASE;
}
}
public void setFlag(int flag) {
System.out.println("Before Set mFlag " + mFlag);
this.mFlag = flag;
System.out.println("After Set mFlag " + mFlag);
}
}
This is my Activity Class for changing brush effect using button click
public class PlayActivity extends Activity{
private DrawingView mDrawingView;
private ViewGroup mBrushPanel;
private ViewGroup mBrushColors;
private SeekBar mBrushStroke;
private ViewGroup brush_panel_pencil;
private ViewGroup brush_colors_pencil;
private SeekBar brush_stroke_pencil;
String imagName ="";
private static String APP_ID = "788092211287311";
// Instance of Facebook Class
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
public boolean flag = false;
String name="";
File file = null;
FileOutputStream fOut = null;
// see:
// http://stackoverflow.com/questions/25758294/how-to-fill-different-color-on-same-area-of-imageview-color-over-another-color/
static int[] COLORS = { Color.rgb(255, 51, 255), // DARK PINK
Color.rgb(255, 230, 102), // LIGHT YELLOW
Color.rgb(148, 66, 50), // DARK MAROON
Color.rgb(186, 123, 68), // LIGHT MAROON
Color.rgb(252, 20, 20), // RED
Color.rgb(102, 255, 255), // LIGHT BLUE
Color.rgb(70, 78, 202), // DARK BLUE
Color.rgb(190, 255, 91), // LIGHT GREEN
Color.rgb(15, 230, 0), // DARK GREEN
Color.rgb(123, 0, 230), // JAMBLI
Color.rgb(255, 187, 50), // ORANGE
Color.rgb(7, 5, 0), // BLACK
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setBackgroundDrawable(
Utils.createCheckerBoard(getResources(), 16));
setContentView(R.layout.activity_play);
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
// mDrawingView.setShape(R.drawable.img_a_inner, R.drawable.img_a);
mDrawingView.setShape(R.drawable.inner, R.drawable.outer);
mDrawingView.setDrawingColor(getResources().getColor(R.color.ab_color));
mDrawingView.setFlag(0);
mBrushPanel = (ViewGroup) findViewById(R.id.brush_panel);
mBrushColors = (ViewGroup) findViewById(R.id.brush_colors);
mBrushStroke = (SeekBar) findViewById(R.id.brush_stroke);
mBrushStroke
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
#Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
mDrawingView.setDrawingStroke(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
});
mBrushStroke.setProgress(30);
mBrushPanel.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
mBrushPanel.getViewTreeObserver()
.removeOnPreDrawListener(this);
mBrushPanel
.setTranslationY(isLandscape() ? -mBrushPanel
.getHeight() : mBrushPanel.getHeight());
return false;
}
});
createBrushPanelContent();
}
#SuppressWarnings("null")
private void createBrushPanelContent() {
TableRow tableRow = null;
final int rowLimit = isLandscape() ? 16 : 8;
for (int i = 0; i < COLORS.length; i++) {
if ((i % rowLimit) == 0) {
tableRow = new TableRow(this);
mBrushColors.addView(tableRow, new TableLayout.LayoutParams(
WRAP_CONTENT, WRAP_CONTENT));
}
tableRow.addView(createToolButton(tableRow,
R.drawable.ic_paint_splot, i));
}
/*tableRow.addView(createToolButton1(tableRow, R.drawable.ic_paint_splot,
1));*/
}
private void showBrushPanel() {
mBrushPanel.animate().translationY(0).start();
}
private void hideBrushPanel() {
mBrushPanel
.animate()
.translationY(
isLandscape() ? -mBrushPanel.getHeight() : mBrushPanel
.getHeight()).start();
}
private boolean isLandscape() {
return getResources().getBoolean(R.bool.is_landscape);
}
private ImageButton createToolButton(ViewGroup parent, int drawableResId,
int index) {
ImageButton button = (ImageButton) getLayoutInflater().inflate(
R.layout.button_paint_spot, parent, false);
button.setImageResource(drawableResId);
button.setOnClickListener(mButtonClick);
if (index != -1) {
button.setTag(Integer.valueOf(index));
button.setColorFilter(COLORS[index]);
}
return button;
}
/* private Button createToolButton1(ViewGroup parent, int drawableResId,
int index) {
Button buttonShare = (Button) getLayoutInflater().inflate(
R.layout.button, parent, false);
buttonShare.setOnClickListener(mButtonShare);
return buttonShare;
}
};*/
private View.OnClickListener mButtonClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
// code for set the shadow color
// mDrawingView.mPaintColor.setShadowLayer(10, 10, 5, Color.RED);
mDrawingView.setDrawingColor(COLORS[((Integer) v.getTag())
.intValue()]);
hideBrushPanel();
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_play, menu);
return super.onCreateOptionsMenu(menu) | true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_emboss:
mDrawingView.setFlag(3);
System.out.println("mClick Emboss");
mDrawingView.setDrawingStroke(30);
if (mBrushPanel.getTranslationY() == 0) {
hideBrushPanel();
} else {
showBrushPanel();
}
break;
case R.id.action_watermark:
mDrawingView.setFlag(2);
mDrawingView.setDrawingStroke(30);
BlurMaskFilter mBlur = new BlurMaskFilter(15, BlurMaskFilter.Blur.NORMAL);
mDrawingView.mPaintColor.setMaskFilter(mBlur);
if (mBrushPanel.getTranslationY() == 0) {
hideBrushPanel();
} else {
showBrushPanel();
}
break;
case R.id.action_pencil:
mDrawingView.setFlag(1);
mDrawingView.setDrawingStroke(4);
if (mBrushPanel.getTranslationY() == 0) {
hideBrushPanel();
} else {
showBrushPanel();
}
break;
case R.id.action_brush:
mDrawingView.setFlag(0);
mDrawingView.setDrawingStroke(30);
if (mBrushPanel.getTranslationY() == 0) {
hideBrushPanel();
} else {
showBrushPanel();
}
break;
case R.id.action_eraser:
mDrawingView.enableEraser();
break;
case R.id.action_undo:
mDrawingView.undoOperation();
break;
case R.id.action_redo:
mDrawingView.redoOperation();
break;
case R.id.action_save: {
mDrawingView
.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
mDrawingView.setDrawingCacheEnabled(true);
mDrawingView.buildDrawingCache();
Bitmap viewCache = mDrawingView.getDrawingCache();
Bitmap bitmap = viewCache.copy(viewCache.getConfig(), false);
mDrawingView.setDrawingCacheEnabled(false);
new SaveTask().execute(bitmap);
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);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.horizon,
(ViewGroup) findViewById(R.id.main_relative_output));
RelativeLayout rl = (RelativeLayout) layout
.findViewById(R.id.main_relative_output);
/*AlertDialog builder = new AlertDialog.Builder(
PlayActivity.this).setView(layout).show();*/
//Convert to byte array
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent i = new Intent(PlayActivity.this, SharingScreen.class);
i.putExtra("image",byteArray);
i.putExtra("name", name);
startActivity(i);
// rl.setBackgroundDrawable(bitmapDrawable);
}
break;
case R.id.action_cancel:
mDrawingView.clearDrawing();
// file.delete();
/*if(viewCache.isRecycled()){
viewCache.recycle();
bitmap.recycle();
}*/
break;
/*case R.id.action_share:
mDrawingView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
mDrawingView.setDrawingCacheEnabled(true);
mDrawingView.buildDrawingCache();
Bitmap viewCache = mDrawingView.getDrawingCache();
Bitmap bitmap = viewCache.copy(viewCache.getConfig(), false);
new SaveAndShare().execute(bitmap);
break;*/
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
Please Help to solve this problem...
First Fragment
Here this is a 1stFragment file sends the string of encoded bitmap
tmp = getArguments().getString("PHOTO");
byte [] encodeByte=Base64.decode(tmp,Base64.DEFAULT);
bmp=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
ByteArrayOutputStream b=new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG,100, b);
bm =b.toByteArray();
temp=Base64.encodeToString(bm, Base64.DEFAULT);Fragment fr = new CropToolActivity();
fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
args = new Bundle();
args.putString("PHOTO", temp);
fr.setArguments(args);
ft.replace(R.id.frame,fr);
ft.commit();
CropToolActivity
Here this is a 2ndFragment file receives the string of encoded bitmap
also set the CropView ti the layout
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.croptool, container, false);
myCropView = (CropView)view.findViewById(R.id.crop_tool);
myCropView = new CropView(getActivity().getApplicationContext());
tmp = getArguments().getString("PHOTO");
byte [] encodeByte=Base64.decode(tmp,Base64.DEFAULT);
bmp=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
myCropView.setImageBitmap(bmp);
croptool.xml
This is a XML file for 2nd Fragment layout
<com.tcss.photostyle.CropView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/crop_tool" />
</LinearLayout>
CropView.Java
Here this is a croptool java file to demonstrate the Croptool on bitmap
public class CropView extends ImageView {
Paint paint = new Paint();
private int initial_size = 300;
private static Point leftTop, rightBottom, center, previous;
private static final int DRAG= 0;
private static final int LEFT= 1;
private static final int TOP= 2;
private static final int RIGHT= 3;
private static final int BOTTOM= 4;
private int imageScaledWidth,imageScaledHeight;
// Adding parent class constructors
public CropView(Context context) {
super(context);
initCropView();
}
public CropView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
initCropView();
}
public CropView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initCropView();
}
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if(leftTop.equals(0, 0))
resetPoints();
canvas.drawRect(leftTop.x, leftTop.y, rightBottom.x, rightBottom.y, paint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
previous.set((int)event.getX(), (int)event.getY());
break;
case MotionEvent.ACTION_MOVE:
if(isActionInsideRectangle(event.getX(), event.getY())) {
adjustRectangle((int)event.getX(), (int)event.getY());
invalidate(); // redraw rectangle
previous.set((int)event.getX(), (int)event.getY());
}
break;
case MotionEvent.ACTION_UP:
previous = new Point();
break;
}
return true;
}
private void initCropView() {
paint.setColor(Color.YELLOW);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(5);
leftTop = new Point();
rightBottom = new Point();
center = new Point();
previous = new Point();
}
public void resetPoints() {
center.set(getWidth()/2, getHeight()/2);
leftTop.set((getWidth()-initial_size)/2,(getHeight()-initial_size)/2);
rightBottom.set(leftTop.x+initial_size, leftTop.y+initial_size);
}
private static boolean isActionInsideRectangle(float x, float y) {
int buffer = 10;
return (x>=(leftTop.x-buffer)&&x<=(rightBottom.x+buffer)&& y>=(leftTop.y-buffer)&&y<=(rightBottom.y+buffer))?true:false;
}
private boolean isInImageRange(PointF point) {
// Get image matrix values and place them in an array
float[] f = new float[9];
getImageMatrix().getValues(f);
// Calculate the scaled dimensions
imageScaledWidth = Math.round(getDrawable().getIntrinsicWidth() * f[Matrix.MSCALE_X]);
imageScaledHeight = Math.round(getDrawable().getIntrinsicHeight() * f[Matrix.MSCALE_Y]);
return (point.x>=(center.x-(imageScaledWidth/2))&&point.x<=(center.x+(imageScaledWidth/2))&&point.y>=(center.y-(imageScaledHeight/2))&&point.y<=(center.y+(imageScaledHeight/2)))?true:false;
}
private void adjustRectangle(int x, int y) {
int movement;
switch(getAffectedSide(x,y)) {
case LEFT:
movement = x-leftTop.x;
if(isInImageRange(new PointF(leftTop.x+movement,leftTop.y+movement)))
leftTop.set(leftTop.x+movement,leftTop.y+movement);
break;
case TOP:
movement = y-leftTop.y;
if(isInImageRange(new PointF(leftTop.x+movement,leftTop.y+movement)))
leftTop.set(leftTop.x+movement,leftTop.y+movement);
break;
case RIGHT:
movement = x-rightBottom.x;
if(isInImageRange(new PointF(rightBottom.x+movement,rightBottom.y+movement)))
rightBottom.set(rightBottom.x+movement,rightBottom.y+movement);
break;
case BOTTOM:
movement = y-rightBottom.y;
if(isInImageRange(new PointF(rightBottom.x+movement,rightBottom.y+movement)))
rightBottom.set(rightBottom.x+movement,rightBottom.y+movement);
break;
case DRAG:
movement = x-previous.x;
int movementY = y-previous.y;
if(isInImageRange(new PointF(leftTop.x+movement,leftTop.y+movementY)) && isInImageRange(new PointF(rightBottom.x+movement,rightBottom.y+movementY))) {
leftTop.set(leftTop.x+movement,leftTop.y+movementY);
rightBottom.set(rightBottom.x+movement,rightBottom.y+movementY);
}
break;
}
}
private static int getAffectedSide(float x, float y) {
int buffer = 10;
if(x>=(leftTop.x-buffer)&&x<=(leftTop.x+buffer))
return LEFT;
else if(y>=(leftTop.y-buffer)&&y<=(leftTop.y+buffer))
return TOP;
else if(x>=(rightBottom.x-buffer)&&x<=(rightBottom.x+buffer))
return RIGHT;
else if(y>=(rightBottom.y-buffer)&&y<=(rightBottom.y+buffer))
return BOTTOM;
else
return DRAG;
}
public byte[] getCroppedImage() {
BitmapDrawable drawable = (BitmapDrawable)getDrawable();
float x = leftTop.x-center.x+(drawable.getBitmap().getWidth()/2);
float y = leftTop.y-center.y+(drawable.getBitmap().getHeight()/2);
Bitmap cropped = Bitmap.createBitmap(drawable.getBitmap(),(int)x,(int)y,(int)rightBottom.x-(int)leftTop.x,(int)rightBottom.y-(int)leftTop.y);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
cropped.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
}
Here is a method for cropping bitmap from center, assume that newWidth < bitmap.getWidth() and newHeight < bitmap.getHeight()
public Bitmap cropBitmap(Bitmap bitmap, int newWidth, int newHeight) {
double x = (bitmap.getWidth() - newWidth) / 2;
double y = (bitmap.getHeight() - newHeight) / 2;
Bitmap b = Bitmap.createBitmap((int)newWidth, (int)newHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.drawBitmap(bitmap, new Rect((int)x , (int)y, (int)(x + newWidth), (int)(y + newHeight)), new Rect(0, 0, (int)newWidth, (int)newHeight), null);
return b;
}
I am developing an application where i need to crop image . But this image croping should happen on my activity only so i can not use default image cropping concept of android where image will go on another view where user can crop .
What i need is to create custom image cropper rectangle .
I am trying this from long but no luck . Would be glad if any body can help .
Thanks
my mainactivity..
public class MainActivity extends Activity {
private DragRectView view;
private ImageView original;
// private ImageView croppedImage;
/**
*
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (DragRectView) findViewById(R.id.dragRect);
original = (ImageView) findViewById(R.id.image);
// croppedImage = (ImageView) findViewById(R.id.crop_image);
if (null != view) {
view.setOnUpCallback(new DragRectView.OnUpCallback() {
#Override
public void onRectFinished(final Rect rect) {
Toast.makeText(
getApplicationContext(),
"Rect is (" + rect.left + ", " + rect.top + ", "
+ rect.right + ", " + rect.bottom + ")",
Toast.LENGTH_LONG).show();
Bitmap bitmap1 = Bitmap.createScaledBitmap(
((BitmapDrawable) original.getDrawable())
.getBitmap(), original.getWidth(), original
.getHeight(), false);
System.out.println(rect.height() + " "
+ bitmap1.getHeight() + " " + rect.width()
+ " " + bitmap1.getWidth());
if (rect.height() <= bitmap1.getHeight()
&& rect.width() <= bitmap1.getWidth()) {
Bitmap bitmap = Bitmap.createBitmap(bitmap1,
view.getLeft(), view.getTop(), view.getWidth(),
view.getHeight());
System.out
.println("MainActivity.onCreate(...).new OnUpCallback() {...}.onRectFinished() if true");
Intent intent = new Intent(MainActivity.this,
CropImageActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("cropimage", bitmap);
startActivity(intent);
System.out.println("MainActivity.onCreate() ");
}
}
});
}
}
}
cropImageAcivity...
public class CropImageActivity extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crop_image);
imageView = (ImageView) findViewById(R.id.crop_image);
Intent intent = getIntent();
if (intent != null) {
Bundle extras = intent.getExtras();
if (extras != null) {
System.out.println("CropImageActivity.onCreate()");
Bitmap bitmap = extras.getParcelable("cropimage");
imageView.setImageBitmap(bitmap);
}
}
}
}
DragRectangle...
public class DragRectView extends View {
private Paint mRectPaint;
private int mStartX = 0;
private int mStartY = 0;
private int mEndX = 0;
private int mEndY = 0;
private boolean mDrawRect = false;
private TextPaint mTextPaint = null;
private OnUpCallback mCallback = null;
public interface OnUpCallback {
void onRectFinished(Rect rect);
}
public DragRectView(final Context context) {
super(context);
init();
}
public DragRectView(final Context context, final AttributeSet attrs) {
super(context, attrs);
init();
}
public DragRectView(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
init();
}
/**
* Sets callback for up
*
* #param callback
* {#link OnUpCallback}
*/
public void setOnUpCallback(OnUpCallback callback) {
mCallback = callback;
}
/**
* Inits internal data
*/
private void init() {
mRectPaint = new Paint();
mRectPaint.setColor(Color.GREEN);
mRectPaint.setStyle(Paint.Style.STROKE);
mRectPaint.setStrokeWidth(5);
mTextPaint = new TextPaint();
mTextPaint.setColor(Color.MAGENTA);
mTextPaint.setTextSize(20);
}
#Override
public boolean onTouchEvent(final MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mDrawRect = false;
mStartX = (int) event.getX();
mStartY = (int) event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
final int x = (int) event.getX();
final int y = (int) event.getY();
if (!mDrawRect || Math.abs(x - mEndX) > 5
|| Math.abs(y - mEndY) > 5) {
mEndX = x;
mEndY = y;
invalidate();
}
mDrawRect = true;
break;
case MotionEvent.ACTION_UP:
if (mCallback != null) {
mCallback.onRectFinished(new Rect(Math.min(mStartX, mEndX),
Math.min(mStartY, mEndY), Math.max(mEndX, mStartX),
Math.max(mEndY, mStartX)));
}
invalidate();
break;
default:
break;
}
return true;
}
#Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
if (mDrawRect) {
canvas.drawRect(Math.min(mStartX, mEndX), Math.min(mStartY, mEndY),
Math.max(mEndX, mStartX), Math.max(mEndY, mStartY),
mRectPaint);
canvas.drawText(
" (" + Math.abs(mStartX - mEndX) + ", "
+ Math.abs(mStartY - mEndY) + ")",
Math.max(mEndX, mStartX), Math.max(mEndY, mStartY),
mTextPaint);
}
}
}
mainactvity layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/background_dark"
android:orientation="vertical"
android:weightSum="2" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/image" />
<com.afbb.imagecrop.DragRectView
android:id="#+id/dragRect"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
<ImageView
android:id="#+id/crop_image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/image" />
-->
</RelativeLayout>
cropactivity layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/crop_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher" />
</FrameLayout>
it may help you...
you can try this for flexible and custom crop.....
Intent intent = new Intent("com.android.camera.action.CROP");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setType("image/*");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputY", 600);
intent.putExtra("outputX", 600);
intent.putExtra("scale", "true);
I think that my problem is minor but I can't find solution. I am trying to make application which will help me to make WEB pages and I want to make application which will put graphic element on the screen.
I made drawable png images of rectangles and I can put them on the screen where I want, no problem, but I can not make new graphic/bitmap.
How I can put new graphics object on the screen when I press in meni DIV or iFrame?
public class Objects extends View {
private float X;
private float Y;
private Paint myPaint;
final String C_DIV = "DIV";
final String C_TABLE = "Table";
final String C_IFRAME = "iFrame";
final String C_LIST = "List";
Canvas canvas;
Bitmap divimg = BitmapFactory.decodeResource(getResources(), R.drawable.div);
Bitmap iframeimg = BitmapFactory.decodeResource(getResources(), R.drawable.iframe);
Bitmap img = null;
String objectname = " ";
public Objects(Context context) {
super(context);
myPaint = new Paint();
}
public void HTMLObjects(String object) {
Log.d(object, "see");
if (object.equals(C_DIV)) {
myPaint.setColor(Color.GREEN);
img = Bitmap.createBitmap(divimg);
objectname = "DIV";
}
if (object.equals(C_IFRAME)) {
myPaint.setColor(Color.BLUE);
img = Bitmap.createBitmap(iframeimg);
objectname = "iFrame";
}
}
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
X = (int) event.getX();
Y = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
X = (int) event.getX();
Y = (int) event.getY();
break;
}
return true;
}
public void onDraw(Canvas canvas) {
if (img != null) {
canvas.drawBitmap(img, X - 50, Y - 50, myPaint);
canvas.drawText(objectname, X, Y - 50, myPaint);
}
invalidate();
}
}
This is a class which I am calling from the activity Workspace with
ob.HTMLObjects((String) item.getTitle());
and I am sending information which menu is pressed.
I do not know how to make new/different object/bitmap when I pressed DIV or iFrame.
Try these version. I replaces x,y,img,objectname with according lists:
public class Objects extends View {
private Paint myPaint;
public final String C_DIV="DIV";
public final String C_TABLE="Table";
public final String C_IFRAME="iFrame";
public final String C_LIST="List";
private Canvas canvas;
private Bitmap divimg= BitmapFactory.decodeResource(getResources(), R.drawable.div);
private Bitmap iframeimg=BitmapFactory.decodeResource(getResources(), R.drawable.iframe);
private List<Bitmap> images;
private List<Float> xs;
private List<Float> ys;
private List<String> objectNames;
public Objects(Context context) {
super(context);
myPaint = new Paint();
images = new ArrayList<Bitmap>();
xs = new ArrayList<Float>();
ys = new ArrayList<Float>();
objectNames = new ArrayList<String>();
}
public void HTMLObjects(String object){
Log.d(object, "see");
if (object.equals(C_DIV)) {
myPaint.setColor(Color.GREEN);
images.add(Bitmap.createBitmap(divimg));
objectNames.add("DIV");
xs.add(0F);
ys.add(0F);
}
if (object.equals(C_IFRAME)){
myPaint.setColor(Color.BLUE);
images.add(Bitmap.createBitmap(iframeimg));
objectNames.add("iFrame");
xs.add(0F);
ys.add(0F);
}
}
public boolean onTouchEvent(MotionEvent event) {
int action=event.getAction();
if (xs.isEmpty()) {
return true;
}
int pos = xs.size() - 1;
switch(action) {
case MotionEvent.ACTION_DOWN:
xs.set(pos, event.getX());
ys.set(pos, event.getY());
break;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
xs.set(pos, event.getX());
ys.set(pos, event.getY());
break;
}
return true;
}
public void onDraw(Canvas canvas){
for (int i = 0; i < images.size(); i++) {
float x = xs.get(i);
float y = ys.get(i);
canvas.drawBitmap(images.get(i), x-50, y-50, myPaint);
canvas.drawText(objectNames.get(i), x, y-50, myPaint);
}
invalidate();
}
}
I'm making an application to create signature on file image. Here, I will load the image file from the memory card to the application and use Canvas Paint can create a signature by hand through the draw.
The problem here is the application requirements for customers to sign in a specific location. My code allows customers can sign in anywhere the customer wants, like paint on the windows. Now, I want to be signed in a specific area only... Help me...
This my code
Main.java
public class Longvan extends Activity implements OnClickListener, OnTouchListener {
ImageView choosenImageView;
Button choosePicture;
Button savePicture;
Bitmap bmp;
Bitmap alteredBitmap;
Canvas canvas;
Paint paint;
Matrix matrix;
float downx = 0;
float downy = 0;
float upx = 0;
float upy = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_longvan);
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
choosenImageView = (ImageView) this.findViewById(R.id.ChoosenImageView);
choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
savePicture = (Button) this.findViewById(R.id.SavePictureButton);
savePicture.setOnClickListener(this);
choosePicture.setOnClickListener(this);
choosenImageView.setOnTouchListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_longvan, menu);
return true;
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = event.getX();
downy = event.getY();
break;
case MotionEvent.ACTION_MOVE:
upx = event.getX();
upy = event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
choosenImageView.invalidate();
downx = upx;
downy = upy;
break;
case MotionEvent.ACTION_UP:
upx = event.getX();
upy = event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
choosenImageView.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == choosePicture) {
Intent choosePictureIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 0);
} else if (v == savePicture) {
if (alteredBitmap != null) {
ContentValues contentValues = new ContentValues(3);
contentValues.put(Media.DISPLAY_NAME, "Draw On Me");
Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);
try {
OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);
alteredBitmap.compress(CompressFormat.JPEG, 90, imageFileOS);
Toast t = Toast.makeText(this, "Thank you! Image Saved!", Toast.LENGTH_LONG);
t.show();
} catch (Exception e) {
Log.v("EXCEPTION", e.getMessage());
}
}
}
}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
Uri imageFileUri = intent.getData();
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
canvas = new Canvas(alteredBitmap);
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStrokeWidth(2);
matrix = new Matrix();
canvas.drawBitmap(bmp, matrix, paint);
choosenImageView.setImageBitmap(alteredBitmap);
choosenImageView.setOnTouchListener(this);
} catch (Exception e) {
Log.v("ERROR", e.toString());
}
}
}
}
How about a specific View for inputting Signatures? Something like this: https://github.com/CoatedMoose/CustomViews/tree/master/library/src/com/coatedmoose/customviews
Then, add this setter for the Bitmap:
public void setImage(Bitmap bitmap) {
this.mBitmap = bitmap;
this.invalidate();
}
Or, if you dont want the applied bitmap as part of your signatureview (use it as background, and not as canvas), replace the onDraw method with this:
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
canvas.drawPath(mPath, mPaint);
}
and then just use mSignatureView.setBackgroundResource(R.drawable.background);
Edit:
Just create a Class with the code from coatedMoose, and use it in your layout XML:
<com.example.myapp.gui.views.SignatureView
android:id="#+id/signature"
android:layout_width="match_parent"
android:layout_height="80dp"/>
Somewhere in your code, or even as an XML attribute, you can set the background.
Edit: signatureView class:
package me.other;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* A simple view to capture a path traced onto the screen. Initially intended to be used to captures signatures.
*
* #author Andrew Crichton
* #version 0.1
*/
public class SignatureView extends View {
#SuppressWarnings("unused")
private Path mPath;
private Paint mPaint;
private Paint bgPaint = new Paint(Color.TRANSPARENT);
private Bitmap mBitmap;
private Canvas mCanvas;
private float curX, curY;
private static final int TOUCH_TOLERANCE = 4;
private static final int STROKE_WIDTH = 4;
public SignatureView(Context context) {
super(context);
init();
}
public SignatureView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SignatureView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setFocusable(true);
mPath = new Path();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(STROKE_WIDTH);
}
public void setSigColor(int color) {
mPaint.setColor(color);
}
public void setSigColor(int a, int red, int green, int blue) {
mPaint.setARGB(a, red, green, blue);
}
public boolean clearSignature() {
if (mBitmap != null)
createFakeMotionEvents();
if (mCanvas != null) {
mCanvas.drawColor(Color.TRANSPARENT);
mCanvas.drawPaint(bgPaint);
mPath.reset();
invalidate();
}
else {
return false;
}
return true;
}
public Bitmap getImage() {
return this.mBitmap;
}
public void setImage(Bitmap bitmap) {
this.mBitmap = bitmap;
this.invalidate();
}
#Override
protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
int bitmapWidth = mBitmap != null ? mBitmap.getWidth() : 0;
int bitmapHeight = mBitmap != null ? mBitmap.getWidth() : 0;
if (bitmapWidth >= width && bitmapHeight >= height)
return;
if (bitmapWidth < width)
bitmapWidth = width;
if (bitmapHeight < height)
bitmapHeight = height;
Bitmap newBitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
Canvas newCanvas = new Canvas();
newCanvas.setBitmap(newBitmap);
if (mBitmap != null)
newCanvas.drawBitmap(mBitmap, 0, 0, null);
mBitmap = newBitmap;
mCanvas = newCanvas;
}
private void createFakeMotionEvents() {
MotionEvent downEvent = MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis()+100, MotionEvent.ACTION_DOWN, 1f, 1f ,0);
MotionEvent upEvent = MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis()+100, MotionEvent.ACTION_UP, 1f, 1f ,0);
onTouchEvent(downEvent);
onTouchEvent(upEvent);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap, 0, 0, mPaint);
canvas.drawPath(mPath, mPaint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchDown(x, y);
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
break;
case MotionEvent.ACTION_UP:
touchUp();
break;
}
invalidate();
return true;
}
/**----------------------------------------------------------
* Private methods
**---------------------------------------------------------*/
private void touchDown(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
curX = x;
curY = y;
}
private void touchMove(float x, float y) {
float dx = Math.abs(x - curX);
float dy = Math.abs(y - curY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(curX, curY, (x + curX)/2, (y + curY)/2);
curX = x;
curY = y;
}
}
private void touchUp() {
mPath.lineTo(curX, curY);
if (mCanvas == null) {
mCanvas = new Canvas();
mCanvas.setBitmap(mBitmap);
}
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
}
Some random layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.mysignatureviewapp.views.SignatureView
android:id="#+id/sign"
android:layout_width="match_parent"
android:layout_height="204dp">
</com.example.mysignatureviewapp.views.SignatureView>
</RelativeLayout>
In the onCreate() method:
private SignatureView sign;
//
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mysignatureviewlayout);
sign = (SignatureView) findViewById(R.id.sign);
sign.setBackgroundResource(R.drawable.mybackgrounddrawable);
}
//get your signature when you press, say, the back button
#Override
public void onBackPressed() {
Bitmap mySignature = sign.getImage();
//do something with bitmap
}
Getting both the background and the signature:
BitmapDrawable mySignature = new BitmapDrawable(sign.getImage());
Drawable background = sign.getBackground();
LayerDrawable ld = new LayerDrawable(new Drawable[] { mySignature, background});