Android ImageView控件和SeekBar的问题

移动开发 码拜 8年前 (2016-05-17) 1085次浏览
通过SeekBar控件改变图片的透明度,并用Toast随时图片的透明度,然后发现不管怎么调SeekBar,图片的透明度都始终是1.0,这让本人很费解,高手们指导下。
这是java代码:
package com.example.application.seekbar;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toast toast;
SeekBar seekBar;
ImageView imageView;
Float f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar)findViewById(R.id.seekbar1);
imageView = (ImageView)findViewById(R.id.imageview1);
f = imageView.getAlpha();
toast=Toast.makeText(getApplicationContext(), f.toString(), Toast.LENGTH_SHORT);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
imageView.setImageAlpha(progress);
f = imageView.getAlpha();
toast=Toast.makeText(getApplicationContext(), f.toString(), Toast.LENGTH_SHORT);
toast.show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
这是xml代码:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout 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:orientation=”vertical”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”com.example.application.seekbar.MainActivity”>
<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:id=”@+id/imageview1″
android:src=”@drawable/hello3″/>
<SeekBar
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:id=”@+id/seekbar1″
android:max=”200″
android:progress=”100″/>
</LinearLayout>
解决方案

40

f = imageView.getAlpha();
你代码里面获取的是imageView这个控件的透明值啊,而你设置的是图片的透明度

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android ImageView控件和SeekBar的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)