当前位置: 编程技术>移动开发
本页文章导读:
▪困扰很久的有关问题 挺纠结的 help啊 困扰很久的问题 挺纠结的 help啊。
我自定义了一个Scroller继承了ViewGroup 这个控件实现的功能就像android桌面那种滚动效果,然后我在activity给我自定义的Scroller添加了元素,分成了几页,我.........
▪ RatingBar显示不全的有关问题 RatingBar显示不全的问题
在一个项目中,用到了RatingBar控件,布局用的是RelativeLayout,运行起来,发现星星总是显示不全,要么少一个星,要么高度被遮。原来是自己不小心引起的,布局文件Ra.........
▪ java软引述(softReference)类型在listview中的一个应用 java软引用(softReference)类型在listview中的一个应用
由于java的回收机制,使得程序员对内存的引用的回收是不可预测的。而这里就可以做做文章了,讲解SoftReference在一方面的应用(对于有些技.........
[1]困扰很久的有关问题 挺纠结的 help啊
来源: 互联网 发布时间: 2014-02-18
困扰很久的问题 挺纠结的 help啊。
我自定义了一个Scroller继承了ViewGroup 这个控件实现的功能就像android桌面那种滚动效果,然后我在activity给我自定义的Scroller添加了元素,分成了几页,我想在我添加的某个元素上显示当前的页码,我不知道代码到底在哪里写,该怎么写?
我自定义了一个Scroller继承了ViewGroup 这个控件实现的功能就像android桌面那种滚动效果,然后我在activity给我自定义的Scroller添加了元素,分成了几页,我想在我添加的某个元素上显示当前的页码,我不知道代码到底在哪里写,该怎么写?
//这是自定义的控件 package org.bluemedia.constom; import org.bluemedia.R; import org.bluemedia.activities.Brand_search; 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.Button; import android.widget.LinearLayout; import android.widget.Scroller; public class ScrollLayout extends ViewGroup { 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); } public ScrollLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mScroller = new Scroller(context); mCurScreen = mDefaultScreen; mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { if (changed) { int childLeft = 0; final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { View childView = getChildAt(i); if (childView.getVisibility() != View.GONE) { final int childWidth = childView.getMeasuredWidth(); childView.layout(childLeft, 0, childLeft + childWidth, childView.getMeasuredHeight()); childLeft += childWidth; } else { removeView(childView); } } } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { Log.e(TAG, "onMeasure"); super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int width = MeasureSpec.getSize(widthMeasureSpec); final int widthMode = MeasureSpec.getMode(widthMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { throw new IllegalStateException( "ScrollLayout only canmCurScreen run at EXACTLY mode!"); } // final int heightMode = MeasureSpec.getMode(heightMeasureSpec); // if (heightMode != MeasureSpec.EXACTLY) { // throw new IllegalStateException( // "ScrollLayout only can run at EXACTLY mode!"); // } // 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); } // Log.e(TAG, "moving to screen "+mCurScreen); scrollTo(mCurScreen * width, 0); } /** * * According to the position of current layout * * scroll to the destination page. */ public void snapToDestination() { final int screenWidth = getWidth(); final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth; snapToScreen(destScreen); } public void snapToScreen(int whichScreen) { // get the valid layout page whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1)); if (getScrollX() != (whichScreen * getWidth())) { 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() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); } } @Override public boolean onTouchEvent(MotionEvent event) { 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"); final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000); int velocityX = (int) velocityTracker.getXVelocity(); Log.e(TAG, "velocityX:" + velocityX); if (velocityX > SNAP_VELOCITY && mCurScreen > 0) { Log.e(TAG, "snap left"); snapToScreen(mCurScreen - 1); System.err.println("11"); } else if (velocityX < -SNAP_VELOCITY && mCurScreen < getChildCount() - 1) { Log.e(TAG, "snap right"); snapToScreen(mCurScreen + 1); System.err.println("2"); } else { snapToDestination(); } mTouchState = TOUCH_STATE_REST; break; case MotionEvent.ACTION_CANCEL: mTouchState = TOUCH_STATE_REST; break; } return true; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { Log.e(TAG, "onInterceptTouchEvent-slop:" + mTouchSlop + "; action = " + ev.getAction()); final int action = ev.getAction(); if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { Log.e(TAG, "onInterceptTouchEvent-action:" + action); 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); Log.e(TAG, "onInterceptTouchEvent-move: xDiff = " + xDiff + "; mLastMotionX = " + mLastMotionX); if (xDiff > mTouchSlop) { mTouchState = TOUCH_STATE_SCROLLING; } break; case MotionEvent.ACTION_DOWN: mLastMotionX = x; mLastMotionY = y; Log.e(TAG, "onInterceptTouchEvent-down: x = " + x + "; y = " + y); mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: Log.e(TAG, "onInterceptTouchEvent-up: mLastMotionX = " + mLastMotionX); mTouchState = TOUCH_STATE_REST; break; } return mTouchState != TOUCH_STATE_REST; } }
//这是我的activity scrollLayout = (ScrollLayout)findViewById(R.id.ScrollLayoutTest); ImageView img1=(ImageView)findViewById(R.id.img1); ImageView img2=(ImageView)findViewById(R.id.img2); ImageView img3=(ImageView)findViewById(R.id.img3); img4=(Button)findViewById(R.id.img4); ImageView img5=(ImageView)findViewById(R.id.img5); ImageView img6=(ImageView)findViewById(R.id.img6); ImageView img7=(ImageView)findViewById(R.id.img7); DisplayMetrics dm = new DisplayMetrics(); // 取得窗口属性 getWindowManager().getDefaultDisplay().getMetrics(dm); // 窗口的宽度 screenWidth = dm.widthPixels; // 图片宽度 imgWidth = BitmapFactory.decodeResource(getResources(), R.drawable.bg_pinpai_fenlei).getWidth(); // 获取中间的空隙 paddingWidth = (screenWidth/4-imgWidth)/2; //得到总页数 if(count%pageSize!=0){ page=count/pageSize+1; }else{ page=count/pageSize; } switch (page) { case 2: img3.setVisibility(1); break; case 3: img2.setVisibility(1); img3.setVisibility(1); break; case 4: img5.setVisibility(0); img2.setVisibility(1); img3.setVisibility(1); break; case 5: img6.setVisibility(1); img5.setVisibility(1); img2.setVisibility(1); img3.setVisibility(1); break; case 6: img7.setVisibility(1); img6.setVisibility(1); img5.setVisibility(1); img2.setVisibility(1); img3.setVisibility(1); break; case 1: img4.setText("1"); break; default: img1.setVisibility(1); img7.setVisibility(1); img6.setVisibility(1); img5.setVisibility(1); img2.setVisibility(1); img3.setVisibility(1); break; } img4.setText("1"); int a=0; //循环给控件添加元素 breakFor:for(int i=0;i<page;i++){ TableLayout table =new TableLayout(this); for(int j=0;j<4;j++){//显示4行4列 TableRow row=new TableRow(this); for(int k=0;k<4;k++){ a++; ImageView img=new ImageView(this); img.setPadding(paddingWidth, 20, paddingWidth, 20); img.setImageResource(R.drawable.bg_pinpai_fenlei); row.addView(img); if(count==a){ table.addView(row); scrollLayout.addView(table); break breakFor;//跳到最外层 } } table.addView(row); } scrollLayout.addView(table); }
[2] RatingBar显示不全的有关问题
来源: 互联网 发布时间: 2014-02-18
RatingBar显示不全的问题
在一个项目中,用到了RatingBar控件,布局用的是RelativeLayout,运行起来,发现星星总是显示不全,要么少一个星,要么高度被遮。原来是自己不小心引起的,布局文件RatingBar的控件用了android:padding="10dp"这个属性,把它去掉就可以了。
小结一下,不知你是否也因为自己的不小心遇上类似的问题呢?细心检查下布局文件看看,希望能帮到你!
[3] java软引述(softReference)类型在listview中的一个应用
来源: 互联网 发布时间: 2014-02-18
java软引用(softReference)类型在listview中的一个应用
由于java的回收机制,使得程序员对内存的引用的回收是不可预测的。而这里就可以做做文章了,讲解SoftReference在一方面的应用(对于有些技术,我感觉只学有用的,不学用不到的。这有别于学os原理,比如os原理你最好精通,这样你的编程内功就会很强,具体的招式则到用到时再学习,或者用不到的就没必要去学)。在android编程中,特别是在listview用来显示来自网络的图片等需要异步加载的资源时,往往需要一个优化的机制。第一是异步加载,这个看我的一篇收藏的博客”Android实现ListView异步加载图片”。第二则是既要考虑到手机的内存问题,又要考虑到网络下载的问题,就是当你下载了一张图片,然后显示在listview的一个view上,然后执行往下拉又往回拉这个动作时,那么我们是否重新去下载该图片呢?好处是节省内存,你每次就存着显示在界面上的那几幅图,但是坏处是每次更新都要重新下载图片。所以我们需要一个缓存的机制,当只有图片的url(即图片的引用)不存在缓存中时才去下载。当然,当内存告急时,需要释放一些没引用的图片,这样,SoftReference就可以很大的用武之地了。即将下载下来的图片放在SoftReference中,然后用map,用Key=img_url来存储该软连接。
最新技术文章: