android - canvas.scale() not working properly -
i have following code, when pinch in screen join/split objects.
import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.scalegesturedetector; import android.view.view; import android.view.scalegesturedetector.simpleonscalegesturelistener; import android.view.animation.scaleanimation; import android.widget.relativelayout; public class gdticketgroupview extends relativelayout{ //constants int knumberoftickets = 3; int kticketwidth = 225; int kticketheight = 100; int kmarginleft = 12; int kmargintop = 12; int kmarginright = 12; int kmarginbottom = 12; float kminscale = 0.5f; float knormalscale = 1.0f; float kmaxscale = 1.2f; //variables float mscalefactor = 1; boolean scaling = false; relativelayout gestureview; //temporary float tmpscale = 1; //objects scalegesturedetector scalegesturedetector; gdticketview ticket; public gdticketgroupview(context context) { super(context); //creating gesturedetector scalegesturedetector = new scalegesturedetector(context, new simpleonscalegesturelistener()); //gesture view gestureview = new relativelayout(context); relativelayout.layoutparams params = new layoutparams(layoutparams.match_parent, layoutparams.match_parent); gestureview.setlayoutparams(params); //adding subviews addtickets(context); //only testing setbackgroundcolor(color.red); } public gdticketgroupview(context context, attributeset attrs) { super(context, attrs); //creating gesturedetector scalegesturedetector = new scalegesturedetector(context, new simpleonscalegesturelistener()); //adding subviews addtickets(context); //only testing setbackgroundcolor(color.red); } public gdticketgroupview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); //creating gesturedetector scalegesturedetector = new scalegesturedetector(context, new simpleonscalegesturelistener()); //adding subviews addtickets(context); //only testing setbackgroundcolor(color.red); } @override public boolean ontouchevent(motionevent event) { scalegesturedetector.ontouchevent(event); return true; } @override public void ondraw(canvas canvas) { super.ondraw(canvas); canvas.save(); if (scaling == true){ if (mscalefactor >= kminscale * tmpscale && mscalefactor < kmaxscale * tmpscale){ canvas.scale(knormalscale, mscalefactor); log.d("debug", "mscalefactor >= kminscale * tmpscale && mscalefactor < kmaxscale * tmpscale"+" true"); } } canvas.restore(); log.d("debug", "ondraw running"); } public class simpleonscalegesturelistener extends simpleonscalegesturelistener { @override public boolean onscale(scalegesturedetector detector) { mscalefactor = detector.getscalefactor(); log.d("debug", "mscalefactor "+mscalefactor+", scale running"); scaling = true; invalidate(); return true; } @override public void onscaleend(scalegesturedetector detector) { //expand animation if (mscalefactor >= kminscale){ scaleanimation anim = new scaleanimation(1, 1, 1, 1); startanimation(anim); tmpscale=1; adjusttickets(); } else if (mscalefactor < kminscale){ scaleanimation anim = new scaleanimation(1, 1, mscalefactor, 1); startanimation(anim); tmpscale = kminscale; adjusttickets(); } scaling = false; log.d("debug", "scale have ended"); } } public void adjusttickets(){ //first ticket gdticketview firstticket = (gdticketview)findviewwithtag(1); //last ticket gdticketview lastticket = (gdticketview)findviewwithtag(knumberoftickets); // creating average variables int totaly = firstticket.gettop() + lastticket.gettop(); int parcialy = totaly / (knumberoftickets - 1); // rest of tickets (int = 2; <= knumberoftickets - 1; i++) { gdticketview ticketbeingadjusted = (gdticketview)findviewwithtag(i); relativelayout.layoutparams params = new layoutparams(kticketwidth, kticketheight); params.setmargins(kmarginleft, (i - 1) * parcialy, kmarginright, kmarginbottom); ticketbeingadjusted.setlayoutparams(params); } } public void addtickets(context context){ for(int = 1; <= knumberoftickets; i++){ ticket = new gdticketview(context); relativelayout.layoutparams params = new layoutparams(kticketwidth, kticketheight); params.setmargins(kmarginleft, kmargintop, kmarginright, kmarginbottom); params.addrule(center_horizontal); if (i != 1){ params.setmargins(kmarginleft, kmargintop+kticketheight, kmarginright, kmarginbottom); gdticketview ticket = (gdticketview)findviewwithtag(i-1); if (ticket.getid() == view.no_id){ ticket.setid(i); } params.addrule(relativelayout.align_top, ticket.getid()); } ticket.settag(i); ticket.setbackgroundcolor(color.green); log.d("init", "ticket view " + +" added correctly"); addview(ticket, params); } } }
in ondraw method, whan mscalefactor change, view don't change height, in logcat mscalefactor it's not 1 so, think don't work right.
you call these methods in order:
save() scale() restore()
but don't draw after calling scale()
. code changes scale factor , restores original scale factor of 1,1.
i see use 2 scaleanimation
instances. 1 doesn't since scales 1,1 1,1. other 1 should work though.
Comments
Post a Comment