网友通过本文主要向大家介绍了android5.0,android5.0新特性,android5.0系统下载,android5.0模拟器,android5.0系统手机等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
Android5.0开发范例大全 读书笔记(三),android5.0范例
(二)用户交互
2.14转发触摸事件
1.TouchDelegate很适合简单的触摸转发,它指定任意的矩形区域来向小视图转发触摸事件,其缺点是每个被转发的事件都会转发到代理视图的中间位置
public class TouchDelegateLayout extends FrameLayout { public TouchDelegateLayout(Context context) { this(context, null); } public TouchDelegateLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public TouchDelegateLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private CheckBox mCheckBox; private void init(Context context) { mCheckBox = new CheckBox(context); mCheckBox.setText("tap anywhere"); addView(mCheckBox, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER)); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w != oldw || h != oldh) { Rect rect = new Rect(0, 0, w, h); TouchDelegate touchDelegate = new TouchDelegate(rect, mCheckBox); setTouchDelegate(touchDelegate); } } }
2.自定义触摸转发
在onTouch中改变event事件信息
@Override public boolean onTouch(View v, MotionEvent event) { event.setLocation(event.getX(),event.getY()/2); scrollView.dispatchTouchEvent(event); return true; }
2.15阻止触摸窃贼
1. 调用requestDisallowTouchIntercept()方法,请求父控件不要截获当前的触摸事件
2.16创建拖放视图
拖1.使用DragShadowBuilder构造被拖动视图的外观
2.调用View.startDrag()开启拖动
@Override public boolean onLongClick(View v) { View.DragShadowBuilder shadowBuilder=new View.DragShadowBuilder(v); v.startDrag(null, shadowBuilder, ((ImageView) v).getDrawable(),0); return true; }
放
3.通过OnDragListener.onDrag()监听拖动的事件,可以自定义一个视图实现listener接口,以下为核心代码
@Override public boolean onDrag(View v, DragEvent event) { PropertyValuesHolder pvhX, pvhY; switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: pvhX = PropertyValuesHolder.ofFloat("scaleX", 0.5f); pvhY = PropertyValuesHolder.ofFloat("scaleY", 0.5f); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); setImageDrawable(null); mDropped = false; break; case DragEvent.ACTION_DRAG_ENDED: if (!mDropped) { pvhX = PropertyValuesHolder.ofFloat("scaleX", 1f); pvhY = PropertyValuesHolder.ofFloat("scaleY", 1f); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); mDropped = false; } break; case DragEvent.ACTION_DRAG_ENTERED: pvhX = PropertyValuesHolder.ofFloat("scaleX", 0.75f); pvhY = PropertyValuesHolder.ofFloat("scaleY", 0.75f); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); break; case DragEvent.ACTION_DRAG_EXITED: pvhX = PropertyValuesHolder.ofFloat("scaleX", 0.5f); pvhY = PropertyValuesHolder.ofFloat("scaleY", 0.5f); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); break; case DragEvent.ACTION_DROP: Keyframe frame0=Keyframe.ofFloat(0f,0.75f); Keyframe frame1=Keyframe.ofFloat(0.5f,0f); Keyframe frame2=Keyframe.ofFloat(1f,0.75f); pvhX = PropertyValuesHolder.ofKeyframe("scaleX", frame0,frame1,frame2); pvhY = PropertyValuesHolder.ofKeyframe("scaleY", frame0, frame1, frame2); ObjectAnimator.ofPropertyValuesHolder(this, pvhX, pvhY).start(); setImageDrawable((Drawable) event.getLocalState()); mDropped=true; break; default: return false; } return true; }
2.17构建导航Drawer
1.DrawerLayout只在Android支持库中提供,关键点在于设置gravity的属性
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/middle" android:layout_width="match_parent" android:layout_height="match_parent"/> <ListView android:id="@+id/left" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#555"/> <LinearLayout android:id="@+id/right" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="right" android:orientation="vertical" android:background="#ccc"> <TextView android:layout_width="wrap_content" android:
您可能想查找下面的文章:
- Android 5.0(包含5.0以下版本) 获取栈顶应用程序包名,android包名
- Android5.0开发范例大全 读书笔记(五),android5.0范例
- Android5.0开发范例大全 读书笔记(六),android5.0范例
- Android5.0开发范例大全 读书笔记(三),android5.0范例
- Android5.0开发范例大全 读书笔记(四),android5.0范例
- android 5.0后对于apk 跑32 64 的逻辑
- Android 5.0 Settings源码简要分析
- Android5.0新特性-Material Design
- Android5.0之Toobar的使用
- Android 6.0 系统棉花糖新的特性和功能