标签:code cat close port 读书 source ram hint static
(二)用户交互
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:layout_height="wrap_content" android:text="this is right"/> </LinearLayout> </android.support.v4.widget.DrawerLayout>
2.当DrawerLayout与ActionBar结合时,要使用ActionBarDrawerToggle
mToggle = new ActionBarDrawerToggle(this, mDrawerContainer, R.string.open, R.string.close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); supportInvalidateOptionsMenu(); } @Override public void onDrawerStateChanged(int newState) { super.onDrawerStateChanged(newState); supportInvalidateOptionsMenu(); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); supportInvalidateOptionsMenu(); } };
3.与ToolBar结合时,和之前类似有两个要点,一是主题的风格,二是调用setActionBar(toolbar)方法
2.18在视图之间滑动
1.定义PagerAdapter实现界面的布局,主要方法是instantiateItem()
public class ImagePagerAdapter extends PagerAdapter { private Context mContext; private static final int[] IMAGES = { android.R.drawable.ic_menu_camera, android.R.drawable.ic_menu_add, android.R.drawable.ic_menu_delete, android.R.drawable.ic_menu_share, android.R.drawable.ic_menu_edit }; private static final int[] COLORS = { Color.RED, Color.BLUE, Color.GREEN, Color.GRAY, Color.MAGENTA }; public ImagePagerAdapter(Context context) { super(); mContext = context; } @Override public int getCount() { return IMAGES.length; } @Override public float getPageWidth(int position) { return 0.333f; } @Override public boolean isViewFromObject(View view, Object object) { return (view == object); } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView iv = new ImageView(mContext); iv.setImageResource(IMAGES[position]); iv.setBackgroundColor(COLORS[position]); container.addView(iv); return iv; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
2.将adaper与viewpager适配
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ImagePagerAdapter adapter=new ImagePagerAdapter(this); ViewPager viewPager=new ViewPager(this); viewPager.setAdapter(adapter); setContentView(viewPager); }
2.18使用选项卡导航
1在sample中有一个SlidingTabBasic SDK,首先要将其中的SlidingTabLayout和SlidingTabStrip复制到自己的项目中
2.接着通过TabsPagerAdapter进行内容的适配,核心方法依然是instantiateItem()
private static class TabsPagerAdapter extends PagerAdapter { private Context mContext; public TabsPagerAdapter(Context mContext) { this.mContext = mContext; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "PRIMARY"; case 1: return "SECONDARY"; case 2: return "TERTIARY"; case 3: return "QUATERNARY"; case 4: return "QUINARY"; default: return ""; } } @Override public int getCount() { return 5; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(mContext); imageView.setScaleType(ImageView.ScaleType.CENTER); imageView.setImageResource(R.mipmap.ic_launcher); container.addView(imageView); return imageView; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } }
3.最后在activity中调用即可
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_action_tabs); ViewPager pager = (ViewPager) findViewById(R.id.pager); SlidingTabLayout tabLayout = (SlidingTabLayout) findViewById(R.id.tabs); assert pager != null; pager.setAdapter(new TabsPagerAdapter(this)); assert tabLayout != null; tabLayout.setViewPager(pager); tabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { @Override public int getIndicatorColor(int position) { return Color.WHITE; } @Override public int getDividerColor(int position) { return 0; } }); }
4.Google自带的sample种有很多牛逼的例子,等考完软考,就把官方的文档和那些API例子一个一个怼掉
标签:code cat close port 读书 source ram hint static
原文地址:http://www.cnblogs.com/xx-wqj/p/5986173.html