【问题标题】:Set alpha on ImageView without setAlpha在没有 setAlpha 的情况下在 ImageView 上设置 alpha
【发布时间】:2012-03-13 21:06:05
【问题描述】:

我能否以编程方式设置 ImageView 的 alpha 而无需依赖于引入 setAlpha 的 API 级别 11?

【问题讨论】:

  • 哦.. 在 ImageView 中还有一个 setAlpha(int alpha) 以及从 API 级别 1 工作的 :)

标签: android imageview alpha


【解决方案1】:

ImageView 自 API 级别 1 起就有一个方法 setAlpha(int)。因此您可以在任何 API 级别使用它。
就是API level 11中引入的ViewsetAlpha(float)方法。

【讨论】:

  • 对 SDK 版本使用 setImageAlpha(X) >=16!
  • 我认为最后一条评论更有用。
  • setAlpha(int) 已弃用。
  • 这不可能是正确的答案,因为它要求的是没有 setAlpha() 的方法,所以 f.e.使用纯 XML
【解决方案2】:

我使用代码来设置图像本身的Alpha,而不是视图。这在 API 级别 1 中可用..

public void toggleButton(int i) {
    if (indImageBtnEnabled[i]) {

        int di = getDrawableId(findViewById(myImagebtns[i]));
        Drawable d = this.getResources().getDrawable(di);
        d.setAlpha(25);

        ((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

        indImageBtnEnabled[i] = false;
    } else {
        // findViewById(myImagebtns[i]).setAlpha(1f) << NEEDS API11;
        int di = getDrawableId(findViewById(myImagebtns[i]));
        Drawable d = this.getResources().getDrawable(di);
        d.setAlpha(255);

        ((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

        indImageBtnEnabled[i] = true;
    }
}

【讨论】:

    【解决方案3】:

    我认为(但不确定)您可以将 alpha 动画应用到您的 ImageView,并且只要动画上的 fillAfter() 设置为 true,Image 应该保持在它结束的任何 alpha 级别在。

    【讨论】:

    • 我刚刚找到了更简单的答案:) ...见上文。
    • 那绝对是要走的路
    【解决方案4】:

    您可以仅使用您的图像创建新布局,设置布局所需的 alpha,将其设置为叠加层并在需要时膨胀。

    LayoutInflater inflater = LayoutInflater.from(this);
            View overView = inflater.inflate(R.layout.myAlpahImageLayout, null);
            this.addContentView(overView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.5"
        tools:context=".MainActivity" >
    
        <ImageView
            android:id="@+id/myImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/myImage1" />
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-20
      • 2011-10-08
      • 1970-01-01
      • 2018-02-04
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      相关资源
      最近更新 更多