PercentRelativeLayout百分比参数
ImageView
* app:layout_widthPercent=”50%” * app:layout_heightPercent=”50%” * app:layout_marginTopPercent=”25%” * app:layout_marginLeftPercent=”25%” *
{@code layout_heightPercent} * {@code layout_marginPercent} * {@code layout_marginLeftPercent} * {@code layout_marginTopPercent} * {@code layout_marginRightPercent} * {@code layout_marginBottomPercent} * {@code layout_marginStartPercent} * {@code layout_marginEndPercent} * rowCount行数 columnCount列数
百分比布局
加依赖 compile ‘com.android.support:percent:25.3.0’
<com.zhy.android.percent.support.PercentRelativeLayout
android:id="@+id/rl"
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">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:clipChildren="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<include
layout="@layout/mains"
app:layout_heightPercent="70%w"
app:layout_marginLeftPercent="5%w"
app:layout_marginTopPercent="75%w"
app:layout_widthPercent="90%w"
/>
</com.zhy.android.percent.support.PercentRelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="always">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<TextView
android:id="@+id/t1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp"
android:background="@color/black_trans_20"/>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_margin="5dp"
android:layout_toRightOf="@id/t1"
android:background="@color/black_trans_20"/>
<TextView
android:id="@+id/t3"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_below="@id/t2"
android:layout_margin="5dp"
android:layout_toRightOf="@id/t1"
android:background="@color/black_trans_20"/>
<TextView
android:id="@+id/t4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="@id/t2"
android:layout_margin="5dp"
android:layout_toRightOf="@id/t3"
android:background="@color/black_trans_20"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@android:color/darker_gray"
android:gravity="center"
android:text="3"/>
</LinearLayout>
<GridLayout
android:alignmentMode="alignBounds"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:orientation="horizontal"
android:rowCount="3">
<Button
android:layout_columnWeight="1"
android:id="@+id/one"
android:text="1"/>
<Button
android:layout_columnWeight="1"
android:id="@+id/two"
android:text="2"/>
<Button
android:layout_columnWeight="1"
android:id="@+id/three"
android:text="3"/>
</GridLayout>
</LinearLayout>
</ScrollView>
Handler发送消息
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int currentItem = vp.getCurrentItem();
currentItem++;
if (currentItem == 10) {
currentItem = 0;
}
vp.setCurrentItem(currentItem);
handler.sendEmptyMessageDelayed(100, 2000);
}
};
获取手机屏幕的宽和高
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(UIUtil.getScreenWidth() * 4 / 10, UIUtil.getScreenHeight() * 6 / 10);
//设置居中
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
/**** 重要部分 ******/
//clipChild用来定义他的子控件是否要在他应有的边界内进行绘制。 默认情况下,clipChild被设置为true。 也就是不允许进行扩展绘制。
vp.setClipChildren(false);
//父容器一定要设置这个,否则看不出效果
rl.setClipChildren(false);
vp.setLayoutParams(params);
//为ViewPager设置PagerAdapter
vp.setAdapter(new MyAdapter());
//设置ViewPager切换效果,即实现画廊效果
vp.setPageTransformer(true, new ZoomOutPageTransformer());
//设置预加载数量
vp.setOffscreenPageLimit(2);
//设置每页之间的左右间隔
vp.setPageMargin(100);
handler.sendEmptyMessageDelayed(100, 2000);
//将容器的触摸事件反馈给ViewPager
rl.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 在按下时 取消轮播
handler.removeMessages(100);
break;
// 触摸事件消失 (被抢走)
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
// 抬起后继续轮播
handler.sendEmptyMessageDelayed(100, 2000);
break;
}
return false;
}
});
}
private class MyAdapter extends PagerAdapter {
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return 10;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(ViewpagerActivity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setImageResource(R.mipmap.abc);
container.addView(imageView);
return imageView;
}
}
/**
* 实现的原理是,在当前显示页面放大至原来的MAX_SCALE
* 其他页面才是正常的的大小MIN_SCALE
*/
private class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MAX_SCALE = 1.4f;
private static final float MIN_SCALE = 0.8f;//0.85f
@Override
public void transformPage(View view, float position) {
//setScaleY只支持api11以上
if (position < -1) {
view.setScaleX(MIN_SCALE);
view.setScaleY(MIN_SCALE);
} else if (position <= 1) //a页滑动至b页 ; a页从 0.0 -1 ;b页从1 ~ 0.0
{ // [-1,1]
// Log.e("TAG", view + " , " + position + "");
float scaleFactor = MIN_SCALE + (1 - Math.abs(position)) * (MAX_SCALE - MIN_SCALE);
view.setScaleX(scaleFactor);
//每次滑动后进行微小的移动目的是为了防止在三星的某些手机上出现两边的页面为显示的情况
if (position > 0) {
view.setTranslationX(-scaleFactor * 2);
} else if (position < 0) {
view.setTranslationX(scaleFactor * 2);
}
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
view.setScaleX(MIN_SCALE);
view.setScaleY(MIN_SCALE);
}
}
}
转载请注明原文地址: https://ju.6miu.com/read-667952.html