【问题标题】:Skewing a text view in Android在 Android 中倾斜文本视图
【发布时间】:2012-07-07 16:12:51
【问题描述】:

我希望在我的应用程序中复制以下内容:

如您所见,它基本上是一个按钮,用于增加/减少其中包含的文本视图的值。此按钮将具有三种视觉状态 -> 未按下、减少和增加(如上图所示,用户点击增加箭头,按钮显示在该侧按下)

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

如您所见,我遇到的问题是能够正确倾斜/旋转文本视图,使其在视觉上看起来正确,并且在增加或减少按钮时与按钮一起倾斜。

到目前为止,我已经尝试了两种不同的方法:

  • 创建一个自定义文本视图类,它会覆盖 onDraw() 方法以倾斜画布:

    @Override
    public void onDraw(Canvas canvas) { 
       canvas.save(); 
    
       canvas.skew(0.2f, 0f);
    
       super.onDraw(canvas); 
       canvas.restore();
    }
    
  • 集成Rotate3dAnimation(来源here)并使用许多不同的变体来获得所需的结果,例如:

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

不幸的是,我并没有完全得到反映上面第一张图片的确切结果。我对使用 Z 轴、倾斜、旋转等设置值感到困惑。

我非常感谢任何有这方面经验的人提供的帮助。提前致谢

【问题讨论】:

    标签: android android-widget android-animation android-canvas


    【解决方案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);
    
            }
        }
    

    我得到一个输出:

    我想通过反复试验来更改值可以解决您的问题。 希望对您有所帮助。

    【讨论】:

    • 您好,帕思,感谢您的回答。通过结合这两种方法,就像你说的那样,稍微改变值,我对最终结果很满意。我想我最终会到达那里,但你的回答加快了速度:)
    • 这只是一个错误的名字@JamesGoodwin,但效果很好!
    • @elgoog 我使用相同的代码,我想知道 200 的值对应什么?它是旋转的中心吗?还是它的某种轴线?
    • 你不应该在 onDraw 中使用 setText 和 setTextSize!另外(正如詹姆斯古德温所写)你不应该命名一个 TextView 活动......
    【解决方案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/layout/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);
    

    问候

    【讨论】:

      猜你喜欢
      • 2013-06-01
      • 1970-01-01
      • 2020-04-12
      • 2023-03-23
      • 2013-07-19
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      相关资源
      最近更新 更多