Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skewing a text view in Android

I'm looking to replicate the following within my application:

enter image description here

As you can see, its basically a button which increases/decreases the value of the text view contained within it. This button will have three visual states -> unpressed, decrease and increase (as seen in the image above, the user taps the increase arrows and the button appears pressed in on that side)

Here are my 3 button states currently:

enter image description hereenter image description hereenter image description here

As you can see, the problem I have is being able to correctly skew/rotate the text view so it looks visually correct and appears slanted along with the button when its being increased or decreased.

I have tried two different approaches so far:

  • Create a custom text view class which overrides the onDraw() method to skew the canvas:

    @Override
    public void onDraw(Canvas canvas) { 
       canvas.save(); 
    
       canvas.skew(0.2f, 0f);
    
       super.onDraw(canvas); 
       canvas.restore();
    }
    
  • Integrate the Rotate3dAnimation class (source here) and used many different variations to get the desired result such as:

       Rotate3dAnimation skew = new Rotate3dAnimation(
              30, 0, centerX, centerY, 0, false);
       txtAmount.startAnimation(skew); 
    

Unfortunately, I'm not quite getting the exact result that mirrors the first image above. I'm getting confused with setting values with the Z-axis, skew, rotate etc.

I'd greatly appreciate any help from anyone who has experience with this stuff. Thanks in advance

like image 466
elgoog Avatar asked Jul 07 '12 12:07

elgoog


1 Answers

Well I even tried and I came up with something like this:

 public class DemoActivity extends TextView {
    Context context;
    String firstText = "$120.00";

 public DemoActivity(Context context)
   {
    super(context);
    this.context = context;

   }


    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    setText(firstText);
    setTextSize(30);
    canvas.skew(1.0f, 0.3f);  //you need to change values over here
    Rotate3dAnimation skew = new Rotate3dAnimation(
              -20, 30,200, 200, 0, false);   //here too
    startAnimation(skew);

        }
    }

I got an output as:

enter image description here

I guess changing the values by trial and error can solve your problem. Hope it helps.

like image 145
Parth Doshi Avatar answered Oct 20 '22 08:10

Parth Doshi