android - Custom ImageView fits to its parent width and height -
i've been creating image processing application. image get's blackened(technically drawing on canvas paint
, path
on touchevent
).
here's layout used blackened screen
<?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"> <framelayout android:id="@+id/blacken_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </framelayout>
in blacken activity :
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.drawingpad_layout); drawingpadlayout = (framelayout) findviewbyid(r.id.blacken_imageview); mdrawingview=new drawingview(this); mdrawingview.setbackgrounddrawable(drawable.createfrompath(imagepath)); // getting imagepath previous activity. mdrawingview.setscaletype(scaletype.fit_center);//i tried center, center_inside i.e every scaletype. it's doesn't change imageview background scale type @ drawingpadlayout.addview(mdrawingview); }
q.
no matter scaletype
applied drawingview
. drawingview
background tht's image getting streched fit screen. seems drawingview background tries match it's parent i.e framelayout how should avoid these?.
i want image background shouldn't stretched match screen. should remain intact.
here's custom imageview
used blacken on touch of it.
class drawingview extends imageview { paint mpaint; bitmap mbitmap; canvas mcanvas; path mpath; paint mbitmappaint; public drawingview(context context) { super(context); // todo auto-generated constructor stub mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color.black); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(20); mpath = new path(); mbitmappaint = new paint(); mbitmappaint.setcolor(color.black); } @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 public void draw(canvas canvas) { // todo auto-generated method stub super.draw(canvas); canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); 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; = 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; = y; } } private void touch_up() { mpath.lineto(mx, my); // commit path our offscreen mcanvas.drawpath(mpath, mpaint); // mpaint.setxfermode(new porterduffxfermode(porterduff.mode.screen)); // kill don't double draw mpath.reset(); // mpath= new path(); } @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; } }
as per pskink,
replacing setbackgrounddrawable
setimagedrawable
resolved issue. image
not getting streched more. remains intact.
mdrawingview.setimagedrawable(drawable.createfrompath(imagepath));
Comments
Post a Comment