<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/iv_my1"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:background="#000"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_gravity="bottom"/>
<ImageView
android:id="@+id/iv_my2"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:background="#000"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_gravity="bottom"/>
<ImageView
android:id="@+id/iv_my3"
android:layout_width="0dp"
android:layout_height="150dp"
android:layout_weight="1"
android:background="#000"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_gravity="bottom"/>
<ImageView
android:id="@+id/iv_myhp"
android:layout_width="40dp"
android:layout_height="150dp"
android:background="#000"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_gravity="bottom"/>
</LinearLayout>
</RelativeLayout>上面是由四个ImageView组成的布局,其中我设定了三个可以滑动标记为iv_1/2/3
public class MainActivity extends AppCompatActivity {
private ImageView iv_my1;
private ImageView iv_my2;
private ImageView iv_my3;
private int screenHeightHalf;
private int originalLeft, originalTop, originalRight, originalBottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iniView();
initData();
}
private void initData() {
screenHeightHalf = getScreenHeightHalf(MainActivity.this);
}
private void iniView() {
iv_my1 = (ImageView) findViewById(R.id.iv_my1);
iv_my2 = (ImageView) findViewById(R.id.iv_my2);
iv_my3 = (ImageView) findViewById(R.id.iv_my3);
iv_my1.setOnTouchListener(touchListener);
iv_my2.setOnTouchListener(touchListener);
iv_my3.setOnTouchListener(touchListener);
}
private View.OnTouchListener touchListener = new View.OnTouchListener() {
private int lastX, lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
originalLeft = v.getLeft();
originalTop = v.getTop();
originalRight = v.getRight();
originalBottom = v.getBottom();
break;
case MotionEvent.ACTION_MOVE:
//测试控件移动的位置
int top = v.getTop();
int screenHeight = screenHeightHalf * 2;
Log.d("TAG", "ImageView Top: " + top + ", Screen Height: " + screenHeight);
moveView(v, event.getRawX() - lastX, event.getRawY() - lastY);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
switch (v.getId()){
case R.id.iv_my1:
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
break;
case R.id.iv_my2:
Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show();
break;
case R.id.iv_my3:
Toast.makeText(MainActivity.this, "3", Toast.LENGTH_SHORT).show();
break;
}
// 处理抬起事件
/*if (v.getTop()<500) {
// 返回原始位置
//TODO
Toast.makeText(MainActivity.this, "超过1/2屏幕高度", Toast.LENGTH_SHORT).show();
}else {
v.layout(originalLeft, originalTop, originalRight, originalBottom);
}*/
break;
}
return true;
}
};
private void moveView(View view, float dx, float dy) {
int left = view.getLeft() + (int) dx;
int top = view.getTop() + (int) dy;
int right = view.getRight() + (int) dx;
int bottom = view.getBottom() + (int) dy;
view.layout(left, top, right, bottom);
}
public int getScreenHeightHalf(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return displayMetrics.heightPixels / 2;
}
}其中getScreenHeightHalf方法是获取半屏的高度,可以在Logcat中显示控件的所处高度(由Log.d()打印出)
该代码示例展示了在Android中如何使用RelativeLayout和LinearLayout创建一个包含四个ImageView的布局。ImageView可以通过触摸事件进行滑动,ACTION_DOWN、ACTION_MOVE和ACTION_UP事件被用来处理滑动操作。同时,当ACTION_UP事件触发时,根据ImageView的ID显示不同的Toast消息。
1574

被折叠的 条评论
为什么被折叠?



