Skewing a text view in Android

后端 未结 2 849
无人共我
无人共我 2020-12-14 11:55

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

\"enter

As you

相关标签:
2条回答
  • 2020-12-14 12:24

    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.

    0 讨论(0)
  • 2020-12-14 12:34

    Thanks to Parth Doshi answer. His answer need a little tweaking to run which I'm sharing here to save someone else time.

    First create a class in src folder and write all of three constructors.

    public class TextViewDemo extends TextView {
    
    Context context;
    String text = "TESTING 3DX TOOLS";
    
    public TextViewDemo(Context context) {
        super(context);
        this.context = context;
    }
    
    public TextViewDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }
    
    public TextViewDemo(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        setText(text);
        setTextSize(30);
        canvas.skew(0.5f, 1.0f); // you need to change values over here
        Rotate3dAnimation skew = new Rotate3dAnimation(-50, 30, 0, 0, 0,
                false); // here too
        startAnimation(skew);
    
    }
    

    }

    In you res/layout/my_layout.xml file you can add a tag of your custom made TextView.

    <com.yourpackage.name.TextViewDemo
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Hello World"
    <!-- All parameters and value shall remain same -->
    />
    

    Like any other view, you can create an instance of TextViewDemo in your onCreate() method

    TextViewDemo txtDemo = (TextViewDemo) findViewById(R.id.name);
    

    Regards

    0 讨论(0)
提交回复
热议问题