在倾斜的Android文本视图(Skewing a text view in Android)

2019-06-26 03:22发布

我期待我的应用程序内复制的情况如下:

正如你所看到的,它基本上增加一个按钮/降低它所包含的文本浏览的价值。 该按钮将有三个视觉状态 - >未压制,减少和增加(如图上图中,用户轻敲增加箭头和按钮显示在按下上侧)

下面是我的3个按钮状态目前:

正如你所看到的,这个问题我已经是能够正确地扭曲,所以它看起来视觉上正确,出现与按钮沿倾斜的被增加或减少时/旋转文本视图。

我已经尝试了两种不同的方法至今:

  • 创建将覆盖自定义文本视图类onDraw()方法歪斜画布:

     @Override public void onDraw(Canvas canvas) { canvas.save(); canvas.skew(0.2f, 0f); super.onDraw(canvas); canvas.restore(); } 
  • 整合Rotate3dAnimation (源此处 ),并用于许多不同的变化,以获得所需的结果,例如:

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

不幸的是,我不太得到镜像上面的第一个图像的精确结果。 我得到与Z轴设定值混淆,倾斜,旋转等。

我非常感谢任何人谁拥有这个东西的经历任何帮助。 提前致谢

Answer 1:

嗯,我甚至试过,我想出了这样的事情:

 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);

        }
    }

我得到了一个输出为:

我猜变化的试验和错误的值可以解决你的问题。 希望能帮助到你。



Answer 2:

由于PARTH Doshi的答案。 他的回答需要一些调整来运行它,我在这里共享,以节省别人的时间。

首先,创建于src文件夹中的一类,并写所有的三个构造。

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);

}

}

在你RES /布局/ my_layout.xml文件,你可以添加你定制的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 -->
/>

像任何其他视图,你可以在你的onCreate创建TextViewDemo的一个实例()方法

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

问候



文章来源: Skewing a text view in Android