105.s1-实现自定义归属地提示框的拖拽1

实现归属地提示框的拖拽,布局里面其实上下都有布局,只是只让某一个显示而已,画图的时候首先是测量(onMesure),安放位置(anLayout),绘制(onDraw)

拖拽的过程中,首先获取到起点坐标,再获取移动后的坐标,计算x,y方向的偏移量,再更新图片的位置。重新初始化起点的坐标。

组快的左右上下的坐标的获取getLeft(),getRight(),getTop(),getBottom();对应在图片中如下

布局文件组合控件

activity_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ldw="http://schemas.android.com/apk/res/com.ldw.safe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    

    <TextView 
		style="@style/TitleStyle"
    	android:text="手机设置"
        />
    <com.ldw.safe.view.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	ldw:title="自动更新设置"
    	ldw:desc_on="自动更新已开启"
    	ldw:desc_off="自动更新已关闭"
        />
    <com.ldw.safe.view.SettingItemView
        android:id="@+id/siv_address"
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
    	ldw:title="电话归属地显示设置"
    	ldw:desc_on="电话归属地显示已开启"
    	ldw:desc_off="电话归属地显示已关闭"
        />
    <com.ldw.safe.view.SettingClickView
        android:id="@+id/scv_address_style"
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
        />
    <com.ldw.safe.view.SettingClickView
        android:id="@+id/scv_address_location"
        android:layout_width="match_parent"
    	android:layout_height="wrap_content"
        />
</LinearLayout>

设置页面的逻辑,点击以后直接跳转

SettingActivity.java

package com.ldw.safe.Activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.ldw.safe.R;
import com.ldw.safe.service.AddressService;
import com.ldw.safe.utils.ServiceStatusUtils;
import com.ldw.safe.view.SettingClickView;
import com.ldw.safe.view.SettingItemView;

/**
 * 设置中心
 */
public class SettingActivity extends Activity {

	private SettingItemView siv_update;//设置自动更新
	private SettingItemView siv_address;//归属地
	private SettingClickView scv_address_style;//归属地显示的风格
	private SettingClickView scv_address_location;//初始化归属地的位置
	
	private SharedPreferences mPref;//把设置的数据保存在mPref
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        
        //把设置的数据保存在mPref
        mPref = getSharedPreferences("config", MODE_PRIVATE);
               
        initUpdateView();//初始化自动升级开关
        initAdressView();//初始化归属地开关
        initAddressStyle();//初始化归属地样式的选择
        initAddressLocation();//初始化归属地出现的位置
	}
	
	/*
	 * 初始化自动升级开关
	 */
	private void initUpdateView(){
		siv_update = (SettingItemView)findViewById(R.id.siv_update);
        //siv_update.setTitle("自动更新设置");
        
        //获取保存的数据,判断之前选择的是开始还是关闭,初始化进入界面是否勾选
        boolean autoUpdate = mPref.getBoolean("auto_update", true);
        if(autoUpdate){
        	siv_update.setDesc("自动更新已经开启");
        	siv_update.setChecked(true);
        }else{
        	siv_update.setDesc("自动更新已经关闭");
        	siv_update.setChecked(false);
        }
        
        siv_update.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				//判断右边框的勾选状态
				if(siv_update.isChecked()){
					//设置不勾选
					siv_update.setChecked(false);
					//siv_update.setDesc("自动更新已经关闭");
					//编辑mPref的值
					mPref.edit().putBoolean("auto_update", false).commit();
				}else{
					//设置勾选
					siv_update.setChecked(true);
					//siv_update.setDesc("自动更新已经开启");
					//编辑mPref的值
					mPref.edit().putBoolean("auto_update", true).commit();
				}
			}
        	
        });
	}
	
	/*
	 * 初始化归属地开关
	 */
	private void initAdressView(){
		
		siv_address = (SettingItemView) findViewById(R.id.siv_address);
		//判断归属地的服务是否在运行
		boolean serviceRunning = ServiceStatusUtils.isServiceRunning(this, "com.ldw.safe.service.AddressService");
		//让服务的那个勾选框根据系统中是否有服务来判断是否去开启
		if(serviceRunning){
			siv_address.setChecked(true);
		}else{
			siv_address.setChecked(false);
		}
		
		siv_address.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(siv_address.isChecked()){
					siv_address.setChecked(false);
					stopService(new Intent(SettingActivity.this, AddressService.class));
				}else{
					siv_address.setChecked(true);
					//号码归属地显示开启的时候要开启服务
					startService(new Intent(SettingActivity.this, AddressService.class));
				}
				
			}
			
		});
	}
	
	/*
	 * 修改号码归属地提示框的风格
	 */
	private void initAddressStyle(){
		scv_address_style = (SettingClickView) findViewById(R.id.scv_address_style);
		
		scv_address_style.setTitle("归属地提示框风格");
		//获取保存的样式
		int style = mPref.getInt("address_Style", 0);
		//设置样式
		scv_address_style.setDesc(items[style]);
		
		scv_address_style.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showSingleChooseDialog();
			}
			
		});
	}
	
	final String[] items = new String[]{"半透明", "活力橙", "卫士蓝", "金属灰", "苹果绿"};
	/*
	 * 弹出归属地样式选择的单选框
	 */
	public void showSingleChooseDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		//设置选择框额logo
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("归属地提示框风格");
		//获取保存的风格样式,默认为0
		int style = mPref.getInt("address_Style", 0);
		
		
		builder.setSingleChoiceItems(items, style, 
				new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				//保存选择的样式的序号
				mPref.edit().putInt("address_Style", which).commit();
				//点击以后对话框消失
				dialog.dismiss();
				//设置组合控件的文字描述
				scv_address_style.setDesc(items[which]);
				
			}
			
		});
		builder.setNegativeButton("取消", null);
		builder.show();
	}
	
	/*
	 * 初始化归属地出现的位置
	 */
	private void initAddressLocation(){
		scv_address_location = (SettingClickView) findViewById(R.id.scv_address_location);
		
		scv_address_location.setTitle("归属地提示框的显示位置");
		scv_address_location.setDesc("设置归属地提示框的显示位置");
		
		scv_address_location.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				//点击以后直接跳转页面
				startActivity(new Intent(SettingActivity.this, DragViewActivity.class));
			}
			
		});
	}

}

点击组合控件以后会跳转页面

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: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>

拖拽页面的业务逻辑,这个里面一开始的iv_drag.layout这个调用需要测量,而后面的监听的回掉函数iv_drag.layout这个又不需要测量。是因为前一个初始化的时候是在onCreate中执行的,还没有测量,因此需要测量一下,监听的时候onCreate已经初始化完成了,因此不需要测量了。 测量的时候是基于父类的控件进行测量的。

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);
		// 获取布局对象,里面包含一系列的参数,比如边距等等
		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;

					// 更新界面
					iv_drag.layout(l, t, r, b);
                    //重新初始化起点坐标
					startX = (int) motionEvent.getRawX();
                    startY = (int) motionEvent.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;
			}
     			
     	});
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值