作者:天眞啲笨尛孩 | 来源:互联网 | 2023-08-12 11:34
在800×480分辨率下view大小如下在1024×600分辨率下view大小却又变成这样http:hi.csdn.netattachment20110725773713
在800×480分辨率下view大小如下
package com.lemote.ebag.UI.glide;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;
/**
*
* @author mahuayi
* 该类主要负责将该类通过addView()方法加入的view滑动时根据用户滑动的速率来决定是否跳转至下一个view
* 当用户需要在下面添加一个view跟随着该类中view的滑动而变化时,须实现该类中的一个接口,在接口中的方法中来控制随着变化的
* view所需做的处理
*
*/
public class ScrollLayout extends ViewGroup {
public onCurScreenChangedListener onCSCL;
private static final String TAG = "ScrollLayout";
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mCurScreen;
private int mDefaultScreen = 0;
private static final int TOUCH_STATE_REST = 0;
private static final int TOUCH_STATE_SCROLLING = 1;
private static final int SNAP_VELOCITY = 600;
private int mTouchState = TOUCH_STATE_REST;
private int mTouchSlop;
private float mLastMotionX;
private float mLastMotionY;
public ScrollLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
mScroller = new Scroller(context);
mCurScreen = mDefaultScreen;
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();//ViewConfiguration.get(Context context)返回指定的上下文的配置
Log.i(TAG,"oncreat");
}
public interface onCurScreenChangedListener{
public void showNext();
public void showPrevious();
}
public onCurScreenChangedListener getOnCurScreenChangedListener(){
return onCSCL;
}
public void setOnCurScreenChangedListener(onCurScreenChangedListener onCSCL){
this.onCSCL = onCSCL;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
Log.e(TAG, "onLayout");
if (changed) {
int childLeft = 0;
final int childCount = getChildCount();
for (int i=0; i
final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
childView.layout(childLeft, 0,
childLeft+childWidth, childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.e(TAG, "onMeasure");
//super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
// The children are given the same width and height as the scrollLayout
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
scrollTo(mCurScreen * width, 0);
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
//this.setMeasuredDimension(parentWidth, parentHeight-10);
//this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
//this.setLayoutParams(new LinearLayout.LayoutParams(parentWidth,parentHeight-10));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
/**
* According to the position of current layout
* scroll to the destination page.
*/
public void snapToDestination() {
final int screenWidth = getWidth();
int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;
destScreen = destScreen >= getChildCount() ? (getChildCount() - 1) : destScreen;
snapToScreen(destScreen);
}
public void snapToScreen(int whichScreen) {
// get the valid layout page
Log.d("snapToScreen", "mCurScreen: " + mCurScreen + "; whichScreen: " + whichScreen);
if(mCurScreen < whichScreen && onCSCL != null){//如果有随着滑动而变化的view,则调用接口的方法
onCSCL.showNext();
}else if(mCurScreen > whichScreen && onCSCL != null){
onCSCL.showPrevious();
}
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
Log.i("getScrollX",getScrollX()+"");
if (getScrollX() != (whichScreen*getWidth())) {//实现回弹的功能(如果没有成功拖至下一个view,则出现回弹效果)
final int delta = whichScreen*getWidth()-getScrollX();
mScroller.startScroll(getScrollX(), 0,
delta, 0, Math.abs(delta)*2);
mCurScreen = whichScreen;
invalidate(); // Redraw the layout
}
}
public void setToScreen(int whichScreen) {
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));
mCurScreen = whichScreen;
scrollTo(whichScreen*getWidth(), 0);
}
public int getCurScreen() {
return mCurScreen;
}
@Override
public void computeScroll() {
// TODO Auto-generated method stub
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "event down!");
if (!mScroller.isFinished()){
mScroller.abortAnimation();
}
mLastMotionX = x;
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int)(mLastMotionX - x);
mLastMotionX = x;
scrollBy(deltaX, 0);
break;
case MotionEvent.ACTION_UP:
Log.e(TAG, "event : up");
// if (mTouchState == TOUCH_STATE_SCROLLING) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
int velocityX = (int) velocityTracker.getXVelocity();
Log.e(TAG, "velocityX:"+velocityX);
if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
// Fling enough to move left
Log.e(TAG, "snap left");
snapToScreen(mCurScreen - 1);
} else if (velocityX < -SNAP_VELOCITY
&& mCurScreen < getChildCount() - 1) {
// Fling enough to move right
Log.e(TAG, "snap right");
snapToScreen(mCurScreen + 1);
} else {
snapToDestination();
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
// }
mTouchState = TOUCH_STATE_REST;
break;
case MotionEvent.ACTION_CANCEL:
mTouchState = TOUCH_STATE_REST;
break;
}
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
Log.e(TAG, "onInterceptTouchEvent-slop:"+mTouchSlop);
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE) &&
(mTouchState != TOUCH_STATE_REST)) {
return true;
}
final float x = ev.getX();
final float y = ev.getY();
switch (action) {
case MotionEvent.ACTION_MOVE:
final int xDiff = (int)Math.abs(mLastMotionX-x);
if (xDiff>mTouchSlop) {
mTouchState = TOUCH_STATE_SCROLLING;
}
break;
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mTouchState = TOUCH_STATE_REST;
break;
}
return mTouchState != TOUCH_STATE_REST;
}
}
12 个解决方案
呵呵……问题是在800×480分辨率下,大小如图片1所示,高度为296,而在1024×600分辨率下,大小如图片2所示,高度为426,不知道为什么高度会发生变化。在xml文件中怎样调整都是那么大。
最快的方法就是在onMeasure里将heightMeasureSpec修改一下就行了。
呵呵……九宫格是作为一个子view添加至ScrollLayout这个自定义的ViewGroup中的,在布局文件中,只有ScrollLayout与数字所在的Layout这两个组件。尝试过在布局文件中调整过间距,但没有效果。
楼主如果用xml文件布局的话,可以尝试针对不同分辨率设置不同的layout
即另外新建几个layout文件夹,原来的layout文件夹为默认的分辨率,保持不变即可,另外新建几个文件夹名为:
layout-480x320, layout-320x240, layout-240x400等等,
系统会自动根据自身的分辨率选择布局,如果找不到合适的则选择layout布局。就像做国际化的方式一样: