问题描述
我想要实现的效果是图片中的效果:
What I'd like to achieve is effect like the one in the picture:
所以基本上有一个 TextView
慢慢消失了.我不希望整个 View
都做成例如50%透明,而例如让它的左部分变成0%透明,而在右边平滑地变成100%透明.
So basically there's a TextView
which slowly disappears. I don't want whole View
to be made for example 50% transparent, but rather for example left part of it to be 0% transparent, and it smoothly goes into 100% transparency at the right side.
我知道某些组件(例如 ListView
)使用了此功能,但是是否可以以非常简单的方式手动进行操作?
I know that some components (e.g. ListView
) uses this, but is it possible to do it manually in quite easy way?
预先感谢答案
推荐答案
首先,创建一个具有alpha渐变的形状.将XML放入您的可绘制目录.我们称它为gradient_view.xml
First, create a shape with an alpha gradient. Place the XML into your drawable directory. Let's call it gradient_view.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:startColor="#FFFFFFFF"
android:endColor="#00FFFFFF"
android:type="linear"/>
</shape>
现在,在您的TextView右侧的布局中创建一个视图.您需要适当地设置宽度和高度,并根据需要在视图中定位(带有layout_toTheRightOf的RelativeLayout可以很好地工作).
Now create a view in your layout to the right of your TextView. You'll need to set width and height appropriately and position in your view as required (RelativeLayout with layout_toTheRightOf would work well).
<View android:layout_width="100dp"
android:layout_height="50dp"
android:background="@drawable/gradient_view"/>
在代码的正确位置进行动画处理.将-200更改为所需的值(甚至更好,找到渐变视图的左X并减去TextView的左边缘以找到要移动的量).
In the right place in your code, animate it. Change the -200 to whatever you need (or even better, find the left X of your gradient view and subtract the left edge of your TextView to find the amount to move).
TranslationAnimation anim = new TranslateAnimation(0, -200, 0, 0);
anim.setDuration(1000);
myTextView.startAnimation(anim);
http://developer.android.com/reference/android/view/animation/TranslateAnimation.html
还有很多工作要做,但这是您所需要的大部分.
There's a bit more work to do but this is most of what you need.
祝你好运
这篇关于如何创建部分不可见的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!