【问题标题】:Android alpha and AlphaAnimationAndroid alpha 和 AlphaAnimation
【发布时间】:2015-02-13 11:14:41
【问题描述】:

在 imageAdapter 类中,我在 try/catch 中将图像 alpha 设置为 0.5 handler.imageView.setAlpha(0.5f);。 它是这样制作的,而不是在布局xml中,因为之前版本的android有int而不是float,所以,在catch中,setAlpha是128。

在活动中,我有一个AlphaAnimation(float from, float to),因此我可以在项目之间滑动时进行更平滑的过渡。我将from = 0.5f 设置为与初始值匹配,将to = 1.0f 设置为完全不透明的图像。我真的从中获得了一部分。未选中的项目显示 0.5 的 alpha,但是当它被选中时,它不是 100% 不透明的,只是多一点。

活动代码:

private View antView = null; //Last view seen
...
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
        if(antView != null)
            setItemViewMode(antView, false);
        antView = v;
        setItemViewMode(v, true);
    }
});

//This method is to detect the item selected, and change styles to selected and the view from before
public void setItemViewMode(View item, boolean selected) {
    Animation animationImage;
    ImageView img = (ImageView) item.findViewById(R.id.imageView1);
    if(selected) {
        animationImage = new AlphaAnimation(0.5f, 1.0f);
    }
    else {
        animationImage = new AlphaAnimation(1.0f, 0.5f);
    }
    animationImage.setDuration(250);
    animationImage.setFillAfter(true);

    img.startAnimation(animationImage);
}

如上所述,动画发生了,但是所选图像的 alpha 不是 100% 不透明的。

在动画中设置不透明度可能有问题吗?

【问题讨论】:

  • 您想要 0.0f 到 1.0f 还是 0.5f 到 1.0f?
  • the image starts with 0.5f and when selected changes from that value to 1.0f to see it full opaque, so I understand that starts from 0.5f in the animation
  • 对,你试过设置插值器吗?
  • 不,我是android dev的新手,所以我会一点一点地按照他们告诉我的去做。

标签: android animation opacity alpha


【解决方案1】:

发现了,不知怎么的,我想在添加动画之前,我应该将不透明度设置为1.0f,然后将动画从0.5f变为1.0f,这样就成功了。

原来是这样:

if(selected)
    animationImage = new AlphaAnimation(0.5f, 1.0f);
else
    animationImage = new AlphaAnimation(1.0f, 0.5f);

img.setAlpha(1.0f);
img.startAnimation(animationImage);

【讨论】:

    【解决方案2】:

    试试这个...

    public void setItemViewMode(View item, boolean selected) {
    Animation animationImage;
    ImageView img = (ImageView) item.findViewById(R.id.imageView1);
    if(selected) {
        animationImage = new AlphaAnimation(0f, 1.0f);
    }
    else {
        animationImage = new AlphaAnimation(1.0f, 0f);
    }
    animationImage.setDuration(250);
    animationImage.setFillAfter(true);
    
    img.startAnimation(animationImage);
    

    }

    【讨论】:

    • 刚试了一下,选中的item也是这样,然后传递的item就完全不可见了(0.0f alpha)
    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    相关资源
    最近更新 更多