让activity的显示呈现半透明的效果
<activity android:name="com.ldw.safe.Activity.DragViewActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
></activity>
同时需要在activity_drag_view.xml中设置背景的颜色,上面的效果是透明效果,再加上背景设置成半黑,就成了一个半黑的透明效果
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#5000"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tv_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="@color/black"
android:textSize="20sp"
android:padding="10dp"
android:background="@drawable/call_locate_blue"
android:text="按住提示框任意拖拽\n按手机返回按键立即生效" />
<TextView
android:id="@+id/tv_bottom"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textColor="@color/black"
android:textSize="20sp"
android:padding="10dp"
android:background="@drawable/call_locate_blue"
android:text="按住提示框任意拖拽\n按手机返回按键立即生效" />
<ImageView
android:id="@+id/iv_drag"
android:layout_marginTop="80dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/drag"
/>
</RelativeLayout>
归属地的显示框框实现任意拖动,并注意到了边界,DragViewActivity.java
package com.ldw.safe.Activity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ldw.safe.R;
/*
* 修改归属地的显示位置
*/
public class DragViewActivity extends Activity {
private TextView tv_top;
private TextView tv_bottom;
private ImageView iv_drag;
private int startX;
private int startY;
private SharedPreferences mPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drag_view);
mPref = getSharedPreferences("config", MODE_PRIVATE);
tv_top = (TextView) findViewById(R.id.tv_top);
tv_bottom = (TextView) findViewById(R.id.tv_bottom);
iv_drag = (ImageView) findViewById(R.id.iv_drag);
int lastX = mPref.getInt("lastX", 0);
int lastY = mPref.getInt("lastY", 0);
//获取屏幕的宽度和高度
final int winWidth = getWindowManager().getDefaultDisplay().getWidth();
final int winHeight = getWindowManager().getDefaultDisplay().getHeight();
if(lastY > winHeight/2){
//上边的提示框显示,下边的隐藏
tv_top.setVisibility(View.VISIBLE);
tv_bottom.setVisibility(View.INVISIBLE);
}else{
//上边的提示框隐藏,下边的显示
tv_top.setVisibility(View.INVISIBLE);
tv_bottom.setVisibility(View.VISIBLE);
}
// 获取布局对象,里面包含一系列的参数,比如边距等等
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) iv_drag
.getLayoutParams();
layoutParams.leftMargin = lastX;// 设置左边距
layoutParams.topMargin = lastY;// 设置top边距
// 重新设置位置
iv_drag.setLayoutParams(layoutParams);
// 设置触摸监听
iv_drag.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//获取起始点在屏幕中的坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
//获取结束点的坐标
int endX = (int) event.getRawX();
int endY = (int) event.getRawY();
//计算偏移量
int dx = endX - startX;
int dy = endY - startY;
// 更新左上右下距离
int l = iv_drag.getLeft() + dx;
int r = iv_drag.getRight() + dx;
int t = iv_drag.getTop() + dy;
int b = iv_drag.getBottom() + dy;
//边界判断
if( l < 0 || r > winWidth || t < 0 || b > winHeight -20){
break;
}
if(t > winHeight/2){
//上边的提示框显示,下边的隐藏
tv_top.setVisibility(View.VISIBLE);
tv_bottom.setVisibility(View.INVISIBLE);
}else{
//上边的提示框隐藏,下边的显示
tv_top.setVisibility(View.INVISIBLE);
tv_bottom.setVisibility(View.VISIBLE);
}
// 更新界面
iv_drag.layout(l, t, r, b);
// 重新初始化起点坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
// 编辑保存记录坐标点
Editor edit = mPref.edit();
edit.putInt("lastX", iv_drag.getLeft());
edit.putInt("lastY", iv_drag.getTop());
edit.commit();
break;
}
return true;
}
});
}
}