先新增一個class:需要定義一個可以隨SeekBar移動的元件
新增ImgMoveLayout.java的class,並繼承ViewGroup
設定版面編排:
在activity_main.xml中,新增SeekBar元件,及剛剛定義的ImgMoveLayout元件
在MainActivity.java中,設定隨SeekBar移動的View樣式,並設定SeekBarChange的監聽器
執行結果:
參考來源: 无懈可击之小鱼的博客
新增ImgMoveLayout.java的class,並繼承ViewGroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImgMoveLayout extends ViewGroup { | |
public ImgMoveLayout(Context context) { | |
super(context); | |
} | |
public ImgMoveLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public ImgMoveLayout(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@Override | |
protected void onLayout(boolean b, int i, int i1, int i2, int i3) { | |
} | |
} |
設定版面編排:
在activity_main.xml中,新增SeekBar元件,及剛剛定義的ImgMoveLayout元件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:orientation="vertical" | |
tools:context="com.example.cwartist.seekbarmovelayout.MainActivity"> | |
<com.example.cwartist.seekbarmovelayout.ImgMoveLayout | |
android:id="@+id/ImgMoveLayout" | |
android:layout_width="match_parent" | |
android:layout_height="100dp"/> | |
<SeekBar | |
android:id="@+id/mySeekBar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:max="100"/> | |
</LinearLayout> |
在MainActivity.java中,設定隨SeekBar移動的View樣式,並設定SeekBarChange的監聽器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private ImgMoveLayout ImgMoveLayout; | |
private SeekBar mySeekBar; | |
private ViewGroup.LayoutParams layoutParams; | |
private ImageView ivWalking; | |
private int screenWidth; | |
private float moveStep = 0; | |
private int progressValue; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mySeekBar = (SeekBar) findViewById(R.id.mySeekBar); | |
screenWidth = getWindowManager().getDefaultDisplay().getWidth(); | |
moveStep = (((float) screenWidth-180 )/ 100); | |
// Set the MoveLayout | |
layoutParams = new ViewGroup.LayoutParams(screenWidth-24, 800); | |
ivWalking = new ImageView(this); | |
ImgMoveLayout = (ImgMoveLayout) findViewById(R.id.ImgMoveLayout); | |
ImgMoveLayout.addView(ivWalking,layoutParams); | |
ivWalking.layout((int)(progressValue*moveStep), 20, 125+(int)(progressValue*moveStep), 300); | |
// Init the ImageView of the MoveLayout | |
progressValue = mySeekBar.getProgress(); | |
ivWalking.setBackgroundResource(R.drawable.walkingr); | |
// Set SeekBarChangeListener | |
mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int i, boolean b) { | |
ivWalking.layout((int) (i * moveStep), 20, 125 + (int) (i * moveStep), 300); | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
} | |
}); | |
} | |
} |
執行結果:
參考來源: 无懈可击之小鱼的博客
留言
張貼留言