【问题标题】:How to change the brightness of an image as its Ratingbar's rating is changed?如何在更改 Ratingbar 的评分时更改图像的亮度?
【发布时间】:2023-03-06 13:50:01
【问题描述】:

我是 Android 新手,希望我的问题足够清楚。我在适配器的某个位置有一个图像和一个评级栏,我将它们添加到片段中。我希望图像的亮度随着用户更改 Ratingbar 上的评级而改变,但不知道从哪里开始。 这是我实例化包含它们的布局的函数:

@Override
public Object instantiateItem(ViewGroup container, int position) {

LayoutInflater inflater = (LayoutInflater)container.getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.rmd1_custom_imageslider_layout, null);   

ImageView image=(ImageView) layout.findViewById(R.id.myimage);             
image.setImageResource(CriteriaImages.get(position));

TextView text=(TextView) layout.findViewById(R.id.myImageViewText);             
text.setText("Rate my "+CriteriaNames.get(position)+" today");

RatingBar rating = (RatingBar) layout.findViewById(R.id.ratingCategory);
rating.setId(position);

Button btn=(Button) layout.findViewById(R.id.done_button);
((ViewPager) container).addView(layout);
return layout; 
}

【问题讨论】:

    标签: android image android-fragments brightness ratingbar


    【解决方案1】:

    你必须设置一个监听器,一个 RatingBar.OnRatingBarChangeListener。

    rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
    
        @Override
        public void onRatingBarChanged( .... ) {
    
            //DO YOUR STUFF, like setting the alpha value of your image.
    
        }
    
    }
    

    【讨论】:

    • 非常感谢!你也碰巧知道如何动态改变图像的亮度吗?
    • 您可以为 imageView 或 drawable 设置 alpha 值,如果这就是亮度的意思。将其设置在可绘制对象上可能是最好的,因此您可以为 imageView 设置背景。设置白色背景将其淡化为白色,设置黑色背景将其淡化为黑色。 imageView.getDrawable().setAlpha(0-255);这应该工作:)
    • 太棒了!作品。非常感谢。
    猜你喜欢
    • 2012-10-10
    • 2010-10-11
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多