您的位置:首页 > 移动开发 > Android开发

Android开发拖动条实现照片的切换

2013-12-30 20:41 295 查看




.xml

<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"
 
  tools:context=".MainActivity"
>

 
 
<ImageView 
 
     
android:id="@+id/img"
 
     
android:layout_width="wrap_content"
 
     
android:layout_height="wrap_content"
 
     
android:src="@drawable/pic_0"/>
 
  
 
 
<SeekBar 
 
     
android:id="@+id/seekbar"
 
     
android:layout_width="fill_parent"
 
     
android:layout_height="wrap_content"/>
 
  
 
 
<TextView 
 
     
android:id="@+id/text"
 
     
android:layout_width="fill_parent"
 
     
android:layout_height="wrap_content"/>

</LinearLayout>

.java

package
com.example.seekbardemo2;

import
android.os.Bundle;
import
android.app.Activity;
import
android.view.Menu;
import
android.widget.ImageView;
import
android.widget.SeekBar;
import
android.widget.SeekBar.OnSeekBarChangeListener;
import
android.widget.TextView;

public class MainActivity
extends Activity {

private ImageView
img=null;
private SeekBar
seekbar=null;
private TextView
text=null;
private int data[]=new
int[]{
R.drawable.pic_0,R.drawable.pic_1,
R.drawable.pic_2,R.drawable.pic_3,
R.drawable.pic_4,R.drawable.pic_5,
R.drawable.pic_6,R.drawable.pic_7,
R.drawable.pic_8,R.drawable.pic_9
};
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.img=(ImageView)
super.findViewById(R.id.img);
this.seekbar=(SeekBar)
super.findViewById(R.id.seekbar);
this.seekbar.setMax(9);
this.text=(TextView)
super.findViewById(R.id.text);
this.seekbar.setOnSeekBarChangeListener(new
OnSeekBarChangeListenerImp());
}

public class
OnSeekBarChangeListenerImp implements
OnSeekBarChangeListener{
//拖动过程
public void
onProgressChanged(SeekBar arg0, int arg1, boolean arg2)
{
// TODO Auto-generated method
stub
int
cur=arg0.getProgress();
//设置显示的图片
MainActivity.this.img.setImageResource(MainActivity.this.data[cur]);
MainActivity.this.text.setText("当前第"+(cur+1)+"张"+"
还有"+(MainActivity.this.data.length-cur-1)+"张");
}
//刚开始拖动
public void
onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method
stub
}

public void
onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method
stub
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: