安卓自學筆記:10:用SeekBar實現通過滑塊控制圖片透明度
阿新 • • 發佈:2019-02-10
今兒11.11光棍節,默默的來寫程式碼吧。SeekBar是一個拖動條,和進度條很相似,通過拖動滑塊來改變值。android:thumb="@drawable/ic_launcher"來控制滑塊的圖示,android:progress="255"控制當前值,android:max="255"控制最大值。
主程式碼:
public class MainActivity extends Activity { private SeekBar bar; private ImageView image; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bar=(SeekBar) findViewById(R.id.bar); image=(ImageView) findViewById(R.id.image); bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // SeekBar的監聽事件 image.setAlpha(progress); } }); } }
xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="240sp" android:src="@drawable/p9" /> <SeekBar android:id="@+id/bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="255" android:progress="255" android:thumb="@drawable/ic_launcher" /> </LinearLayout>
效果圖: