如何在不更改后台android的情况下将alpha设置为ListPreference

时间:2014-06-24 13:32:25

标签: android alpha-transparency listpreference

我使用自定义ListPreference。我需要改变它的alpha

这是代码:

public class IconPreference extends ListPreference {

private Drawable mIcon;
private TextView textView;
private TextView titleView;
private float alpha;
public IconPreference(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public IconPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs);
    setLayoutResource(R.layout.icon_pref);
    TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.IconPreference, defStyle, 0);
    mIcon = a.getDrawable(R.styleable.IconPreference_icon);
}

@Override
public void onBindView(View view) {
    super.onBindView(view);
    textView =  (TextView) view.findViewById(android.R.id.summary);
    titleView =  (TextView) view.findViewById(android.R.id.title);
    ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    if (imageView != null && mIcon != null) {
        imageView.setImageDrawable(mIcon);
    }
}

public void setIcon(Drawable icon) {
    if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
        mIcon = icon;
        notifyChanged();
    }
}

public Drawable getIcon() {
    return mIcon;
}

@Override
public void setValue(String value) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        super.setValue(value);
    } else {
        String oldValue = getValue();
        super.setValue(value);
        if (!TextUtils.equals(value, oldValue)) {
            notifyChanged();
        }
    }
}
public void changeSummary(String name)
{
    textView.setText(name);
    titleView.setText(name);

     notifyChanged();
}
public float getAlpha(){
    return alpha;

}


public void setAlpha(int opacity){
    if (this.alpha != opacity) {
     alpha = opacity;
     AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
     alpha.setDuration(0); // Make animation instant
     alpha.setFillAfter(true); // Tell it to persist after the animation ends

     // And then on your layout

     textView.startAnimation(alpha);
     textView.invalidate();
     titleView.startAnimation(alpha);
     titleView.invalidate();

   }

     // And then on your layout

}

}

NullPointerException字符串中的 textView.startAnimation(alpha);

此代码我用于具有许多子元素的自定义RelativeLayout,并且该代码适用于它。

如何将alpha实施到ListPreference

1 个答案:

答案 0 :(得分:0)

我通过使用自定义ListPreference的onBindView中的标志解决了这个问题:

@Override
public void onBindView(View view) {
    super.onBindView(view);
    textView =  (TextView) view.findViewById(android.R.id.title);
    titleView =  (TextView) view.findViewById(android.R.id.summary);
    this.view = view;
    ImageView imageView = (ImageView) view.findViewById(R.id.icon);
    if (imageView != null && mIcon != null) {
        imageView.setImageDrawable(mIcon);
    }
    if (transparancyFlag )
    {
        textView.setTextColor(context.getResources().getColor(R.color.translucent_black));
        titleView.setTextColor(context.getResources().getColor(R.color.translucent_black));
    }

}

我正在使用setAlpha()方法将transparancy设置为指定的ListPreference:

 public void setAlpha(boolean flag){
    if (flag)
        transparancyFlag = true;
    else transparancyFlag = false;
    notifyChanged();
     // And then on your layout

}