Android 自定义UI-垂直方向的SeekBar





http://blog.csdn.net/wangjinyu501/article/details/20456761

http://blog.csdn.net/wangjinyu501/article/details/20456761

http://blog.csdn.net/wangjinyu501/article/details/20456761

http://blog.csdn.net/wangjinyu501/article/details/20456761



 

Android 自定义UI-垂直方向的SeekBar

  6403人阅读  评论(13)  收藏  举报
  分类:

版本:2.0

日期:2014.4.14 2014.5.7
版权:© 2014 kince 转载注明出处
  
方式一

  系统自带的SeekBar样式是水平的,如果需求一个垂直方向的效果就需要自定义了。原理很简单,即定义一个类继承于SeekBar,并在OnDraw方法里面旋转一下视图。

代码如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package android.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.util.AttributeSet;  
  6. import android.util.Log;  
  7. import android.view.MotionEvent;  
  8.   
  9. public class VerticalSeekBar extends SeekBar {  
  10.   
  11.     public VerticalSeekBar(Context context) {  
  12.         super(context);  
  13.     }  
  14.   
  15.     public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {  
  16.         super(context, attrs, defStyle);  
  17.     }  
  18.   
  19.     public VerticalSeekBar(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.     }  
  22.   
  23.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  24.         super.onSizeChanged(h, w, oldh, oldw);  
  25.     }  
  26.   
  27.     @Override  
  28.     protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  29.         super.onMeasure(heightMeasureSpec, widthMeasureSpec);  
  30.         setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());  
  31.     }  
  32.   
  33.     protected void onDraw(Canvas c) {  
  34.         //将SeekBar转转90度  
  35.         c.rotate(-90);  
  36.         //将旋转后的视图移动回来  
  37.         c.translate(-getHeight(),0);  
  38.         Log.i("getHeight()",getHeight()+"");  
  39.         super.onDraw(c);  
  40.     }  
  41.   
  42.     @Override  
  43.     public boolean onTouchEvent(MotionEvent event) {  
  44.         if (!isEnabled()) {  
  45.             return false;  
  46.         }  
  47.   
  48.         switch (event.getAction()) {  
  49.             case MotionEvent.ACTION_DOWN:  
  50.             case MotionEvent.ACTION_MOVE:  
  51.             case MotionEvent.ACTION_UP:  
  52.                 int i=0;  
  53.                 //获取滑动的距离  
  54.                 i=getMax() - (int) (getMax() * event.getY() / getHeight());  
  55.                 //设置进度  
  56.                 setProgress(i);  
  57.                 Log.i("Progress",getProgress()+"");  
  58.                 //每次拖动SeekBar都会调用  
  59.                 onSizeChanged(getWidth(), getHeight(), 00);  
  60.                 Log.i("getWidth()",getWidth()+"");  
  61.                 Log.i("getHeight()",getHeight()+"");  
  62.                 break;  
  63.   
  64.             case MotionEvent.ACTION_CANCEL:  
  65.                 break;  
  66.         }  
  67.         return true;  
  68.     }  
  69.       
  70. }  
  xml布局文件:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:gravity="center"  
  6.     android:background="@android:color/white"  
  7.     android:orientation="horizontal" >  
  8.   
  9.     <android.widget.VerticalSeekBar_Reverse  
  10.         android:id="@+id/seekbar_reverse"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="450dip"  
  13.         android:layout_marginRight="30dip" />  
  14.   
  15.     <TextView  
  16.         android:id="@+id/reverse_sb_progresstext"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_below="@+id/seekbar_reverse"  
  20.         android:gravity="center" />  
  21.   
  22.     <android.widget.VerticalSeekBar  
  23.         android:id="@+id/vertical_Seekbar"  
  24.         android:layout_width="wrap_content"  
  25.         android:layout_height="450dip"  
  26.         android:layout_toRightOf="@+id/seekbar_reverse" />  
  27.   
  28.     <TextView  
  29.         android:id="@+id/vertical_sb_progresstext"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:layout_below="@+id/vertical_Seekbar"  
  33.         android:layout_toRightOf="@+id/seekbar_reverse"  
  34.         android:gravity="center" />  
  35.   
  36. </RelativeLayout>  
  
方式二
  还有一种方式就是对系统的ProgressBar进行修改,代码如下:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. /*  
  2.  *              Copyright (C) 2011 The MusicMod Project  
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License");  
  5.  * you may not use this file except in compliance with the License.  
  6.  * You may obtain a copy of the License at  
  7.  *  
  8.  *            http://www.apache.org/licenses/LICENSE-2.0  
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" BASIS,  
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.  * See the License for the specific language governing permissions and  
  14.  * limitations under the License.  
  15.  */  
  16.   
  17. package org.joggingTunes.android.activities;  
  18.   
  19. import android.content.Context;  
  20. import android.content.res.TypedArray;  
  21. import android.graphics.Bitmap;  
  22. import android.graphics.Canvas;  
  23. import android.graphics.Rect;  
  24. import android.graphics.drawable.BitmapDrawable;  
  25. import android.graphics.drawable.ClipDrawable;  
  26. import android.graphics.drawable.Drawable;  
  27. import android.graphics.drawable.LayerDrawable;  
  28. import android.graphics.drawable.ShapeDrawable;  
  29. import android.graphics.drawable.StateListDrawable;  
  30. import android.graphics.drawable.shapes.RoundRectShape;  
  31. import android.graphics.drawable.shapes.Shape;  
  32. import android.util.AttributeSet;  
  33. import android.view.Gravity;  
  34. import android.view.View;  
  35. import android.view.ViewDebug;  
  36. import android.view.ViewParent;  
  37. import android.widget.RemoteViews.RemoteView;  
  38. import android.os.Parcel;  
  39. import android.os.Parcelable;  
  40.   
  41. @RemoteView  
  42. public class VerticalProgressBar extends View {  
  43.     private static final int MAX_LEVEL = 10000;  
  44.   
  45.     int mMinWidth;  
  46.     int mMaxWidth;  
  47.     int mMinHeight;  
  48.     int mMaxHeight;  
  49.   
  50.     private int mProgress;  
  51.     private int mSecondaryProgress;  
  52.     private int mMax;  
  53.   
  54.     private Drawable mProgressDrawable;  
  55.     private Drawable mCurrentDrawable;  
  56.     Bitmap mSampleTile;  
  57.     private boolean mNoInvalidate;  
  58.     private RefreshProgressRunnable mRefreshProgressRunnable;  
  59.     private long mUiThreadId;  
  60.   
  61.     private boolean mInDrawing;  
  62.   
  63.     protected int mScrollX;  
  64.     protected int mScrollY;  
  65.     protected int mPaddingLeft;  
  66.     protected int mPaddingRight;  
  67.     protected int mPaddingTop;  
  68.     protected int mPaddingBottom;  
  69.     protected ViewParent mParent;  
  70.   
  71.     /**  
  72.      * Create a new progress bar with range 0...100 and initial progress of 0.  
  73.      *   
  74.      * @param context  
  75.      *            the application environment  
  76.      */  
  77.     public VerticalProgressBar(Context context) {  
  78.         this(context, null);  
  79.     }  
  80.   
  81.     public VerticalProgressBar(Context context, AttributeSet attrs) {  
  82.         this(context, attrs, android.R.attr.progressBarStyle);  
  83.     }  
  84.   
  85.     public VerticalProgressBar(Context context, AttributeSet attrs, int defStyle) {  
  86.         super(context, attrs, defStyle);  
  87.         mUiThreadId = Thread.currentThread().getId();  
  88.         initProgressBar();  
  89.   
  90.         TypedArray a = context.obtainStyledAttributes(attrs,  
  91.                 R.styleable.ProgressBar, defStyle, 0);  
  92.   
  93.         mNoInvalidate = true;  
  94.   
  95.         Drawable drawable = a  
  96.                 .getDrawable(R.styleable.ProgressBar_android_progressDrawable);  
  97.         if (drawable != null) {  
  98.             drawable = tileify(drawable, false);  
  99.             // Calling this method can set mMaxHeight, make sure the  
  100.             // corresponding  
  101.             // XML attribute for mMaxHeight is read after calling this method  
  102.             setProgressDrawable(drawable);  
  103.         }  
  104.   
  105.         mMinWidth = a.getDimensionPixelSize(  
  106.                 R.styleable.ProgressBar_android_minWidth, mMinWidth);  
  107.         mMaxWidth = a.getDimensionPixelSize(  
  108.                 R.styleable.ProgressBar_android_maxWidth, mMaxWidth);  
  109.         mMinHeight = a.getDimensionPixelSize(  
  110.                 R.styleable.ProgressBar_android_minHeight, mMinHeight);  
  111.         mMaxHeight = a.getDimensionPixelSize(  
  112.                 R.styleable.ProgressBar_android_maxHeight, mMaxHeight);  
  113.   
  114.         setMax(a.getInt(R.styleable.ProgressBar_android_max, mMax));  
  115.   
  116.         setProgress(a.getInt(R.styleable.ProgressBar_android_progress,  
  117.                 mProgress));  
  118.   
  119.         setSecondaryProgress(a.getInt(  
  120.                 R.styleable.ProgressBar_android_secondaryProgress,  
  121.                 mSecondaryProgress));  
  122.   
  123.         mNoInvalidate = false;  
  124.   
  125.         a.recycle();  
  126.     }  
  127.   
  128.     /**  
  129.      * Converts a drawable to a tiled version of itself. It will recursively  
  130.      * traverse layer and state list drawables.  
  131.      */  
  132.     private Drawable tileify(Drawable drawable, boolean clip) {  
  133.   
  134.         if (drawable instanceof LayerDrawable) {  
  135.             LayerDrawable background = (LayerDrawable) drawable;  
  136.             final int N = background.getNumberOfLayers();  
  137.             Drawable[] outDrawables = new Drawable[N];  
  138.   
  139.             for (int i = 0; i < N; i++) {  
  140.                 int id = background.getId(i);  
  141.                 outDrawables[i] = tileify(  
  142.                         background.getDrawable(i),  
  143.                         (id == android.R.id.progress || id == android.R.id.secondaryProgress));  
  144.             }  
  145.   
  146.             LayerDrawable newBg = new LayerDrawable(outDrawables);  
  147.   
  148.             for (int i = 0; i < N; i++) {  
  149.                 newBg.setId(i, background.getId(i));  
  150.             }  
  151.   
  152.             return newBg;  
  153.   
  154.         } else if (drawable instanceof StateListDrawable) {  
  155.             StateListDrawable out = new StateListDrawable();  
  156.             return out;  
  157.   
  158.         } else if (drawable instanceof BitmapDrawable) {  
  159.             final Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();  
  160.             if (mSampleTile == null) {  
  161.                 mSampleTile = tileBitmap;  
  162.             }  
  163.   
  164.             final ShapeDrawable shapeDrawable = new ShapeDrawable(  
  165.                     getDrawableShape());  
  166.             return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT,  
  167.                     ClipDrawable.HORIZONTAL) : shapeDrawable;  
  168.         }  
  169.   
  170.         return drawable;  
  171.     }  
  172.   
  173.     Shape getDrawableShape() {  
  174.         final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };  
  175.         return new RoundRectShape(roundedCorners, null, null);  
  176.     }  
  177.   
  178.     /**  
  179.      * <p>  
  180.      * Initialize the progress bar's default values:  
  181.      * </p>  
  182.      * <ul>  
  183.      * <li>progress = 0</li>  
  184.      * <li>max = 100</li>  
  185.      * </ul>  
  186.      */  
  187.     private void initProgressBar() {  
  188.         mMax = 100;  
  189.         mProgress = 0;  
  190.         mSecondaryProgress = 0;  
  191.         mMinWidth = 24;  
  192.         mMaxWidth = 48;  
  193.         mMinHeight = 24;  
  194.         mMaxHeight = 48;  
  195.     }  
  196.   
  197.     /**  
  198.      * <p>  
  199.      * Get the drawable used to draw the progress bar in progress mode.  
  200.      * </p>  
  201.      *   
  202.      * @return a {@link android.graphics.drawable.Drawable} instance  
  203.      *   
  204.      * @see #setProgressDrawable(android.graphics.drawable.Drawable)  
  205.      */  
  206.     public Drawable getProgressDrawable() {  
  207.         return mProgressDrawable;  
  208.     }  
  209.   
  210.     /**  
  211.      * <p>  
  212.      * Define the drawable used to draw the progress bar in progress mode.  
  213.      * </p>  
  214.      *   
  215.      * @param d  
  216.      *            the new drawable  
  217.      *   
  218.      * @see #getProgressDrawable()  
  219.      */  
  220.     public void setProgressDrawable(Drawable d) {  
  221.         if (d != null) {  
  222.             d.setCallback(this);  
  223.             // Make sure the ProgressBar is always tall enough  
  224.             int drawableHeight = d.getMinimumHeight();  
  225.             if (mMaxHeight < drawableHeight) {  
  226.                 mMaxHeight = drawableHeight;  
  227.                 requestLayout();  
  228.             }  
  229.         }  
  230.         mProgressDrawable = d;  
  231.         mCurrentDrawable = d;  
  232.         postInvalidate();  
  233.     }  
  234.   
  235.     /**  
  236.      * @return The drawable currently used to draw the progress bar  
  237.      */  
  238.     Drawable getCurrentDrawable() {  
  239.         return mCurrentDrawable;  
  240.     }  
  241.   
  242.     @Override  
  243.     protected boolean verifyDrawable(Drawable who) {  
  244.         return who == mProgressDrawable || super.verifyDrawable(who);  
  245.     }  
  246.   
  247.     @Override  
  248.     public void postInvalidate() {  
  249.         if (!mNoInvalidate) {  
  250.             super.postInvalidate();  
  251.         }  
  252.     }  
  253.   
  254.     private class RefreshProgressRunnable implements Runnable {  
  255.   
  256.         private int mId;  
  257.         private int mProgress;  
  258.         private boolean mFromUser;  
  259.   
  260.         RefreshProgressRunnable(int id, int progress, boolean fromUser) {  
  261.             mId = id;  
  262.             mProgress = progress;  
  263.             mFromUser = fromUser;  
  264.         }  
  265.   
  266.         public void run() {  
  267.             doRefreshProgress(mId, mProgress, mFromUser);  
  268.             // Put ourselves back in the cache when we are done  
  269.             mRefreshProgressRunnable = this;  
  270.         }  
  271.   
  272.         public void setup(int id, int progress, boolean fromUser) {  
  273.             mId = id;  
  274.             mProgress = progress;  
  275.             mFromUser = fromUser;  
  276.         }  
  277.   
  278.     }  
  279.   
  280.     private synchronized void doRefreshProgress(int id, int progress,  
  281.             boolean fromUser) {  
  282.         float scale = mMax > 0 ? (float) progress / (float) mMax : 0;  
  283.         final Drawable d = mCurrentDrawable;  
  284.         if (d != null) {  
  285.             Drawable progressDrawable = null;  
  286.   
  287.             if (d instanceof LayerDrawable) {  
  288.                 progressDrawable = ((LayerDrawable) d)  
  289.                         .findDrawableByLayerId(id);  
  290.             }  
  291.   
  292.             final int level = (int) (scale * MAX_LEVEL);  
  293.             (progressDrawable != null ? progressDrawable : d).setLevel(level);  
  294.         } else {  
  295.             invalidate();  
  296.         }  
  297.   
  298.         if (id == android.R.id.progress) {  
  299.             onProgressRefresh(scale, fromUser);  
  300.         }  
  301.     }  
  302.   
  303.     void onProgressRefresh(float scale, boolean fromUser) {  
  304.     }  
  305.   
  306.     private synchronized void refreshProgress(int id, int progress,  
  307.             boolean fromUser) {  
  308.         if (mUiThreadId == Thread.currentThread().getId()) {  
  309.             doRefreshProgress(id, progress, fromUser);  
  310.         } else {  
  311.             RefreshProgressRunnable r;  
  312.             if (mRefreshProgressRunnable != null) {  
  313.                 // Use cached RefreshProgressRunnable if available  
  314.                 r = mRefreshProgressRunnable;  
  315.                 // Uncache it  
  316.                 mRefreshProgressRunnable = null;  
  317.                 r.setup(id, progress, fromUser);  
  318.             } else {  
  319.                 // Make a new one  
  320.                 r = new RefreshProgressRunnable(id, progress, fromUser);  
  321.             }  
  322.             post(r);  
  323.         }  
  324.     }  
  325.   
  326.     /**  
  327.      * <p>  
  328.      * Set the current progress to the specified value.  
  329.      * </p>  
  330.      *   
  331.      * @param progress  
  332.      *            the new progress, between 0 and {@link #getMax()}  
  333.      *   
  334.      * @see #getProgress()  
  335.      * @see #incrementProgressBy(int)  
  336.      */  
  337.     public synchronized void setProgress(int progress) {  
  338.         setProgress(progress, false);  
  339.     }  
  340.   
  341.     synchronized void setProgress(int progress, boolean fromUser) {  
  342.         if (progress < 0) {  
  343.             progress = 0;  
  344.         }  
  345.   
  346.         if (progress > mMax) {  
  347.             progress = mMax;  
  348.         }  
  349.   
  350.         if (progress != mProgress) {  
  351.             mProgress = progress;  
  352.             refreshProgress(android.R.id.progress, mProgress, fromUser);  
  353.         }  
  354.     }  
  355.   
  356.     /**  
  357.      * <p>  
  358.      * Set the current secondary progress to the specified value.  
  359.      * </p>  
  360.      *   
  361.      * @param secondaryProgress  
  362.      *            the new secondary progress, between 0 and {@link #getMax()}  
  363.      * @see #getSecondaryProgress()  
  364.      * @see #incrementSecondaryProgressBy(int)  
  365.      */  
  366.     public synchronized void setSecondaryProgress(int secondaryProgress) {  
  367.         if (secondaryProgress < 0) {  
  368.             secondaryProgress = 0;  
  369.         }  
  370.   
  371.         if (secondaryProgress > mMax) {  
  372.             secondaryProgress = mMax;  
  373.         }  
  374.   
  375.         if (secondaryProgress != mSecondaryProgress) {  
  376.             mSecondaryProgress = secondaryProgress;  
  377.             refreshProgress(android.R.id.secondaryProgress, mSecondaryProgress,  
  378.                     false);  
  379.         }  
  380.     }  
  381.   
  382.     /**  
  383.      * <p>  
  384.      * Get the progress bar's current level of progress.  
  385.      * </p>  
  386.      *   
  387.      * @return the current progress, between 0 and {@link #getMax()}  
  388.      *   
  389.      * @see #setProgress(int)  
  390.      * @see #setMax(int)  
  391.      * @see #getMax()  
  392.      */  
  393.     @ViewDebug.ExportedProperty  
  394.     public synchronized int getProgress() {  
  395.         return mProgress;  
  396.     }  
  397.   
  398.     /**  
  399.      * <p>  
  400.      * Get the progress bar's current level of secondary progress.  
  401.      * </p>  
  402.      *   
  403.      * @return the current secondary progress, between 0 and {@link #getMax()}  
  404.      *   
  405.      * @see #setSecondaryProgress(int)  
  406.      * @see #setMax(int)  
  407.      * @see #getMax()  
  408.      */  
  409.     @ViewDebug.ExportedProperty  
  410.     public synchronized int getSecondaryProgress() {  
  411.         return mSecondaryProgress;  
  412.     }  
  413.   
  414.     /**  
  415.      * <p>  
  416.      * Return the upper limit of this progress bar's range.  
  417.      * </p>  
  418.      *   
  419.      * @return a positive integer  
  420.      *   
  421.      * @see #setMax(int)  
  422.      * @see #getProgress()  
  423.      * @see #getSecondaryProgress()  
  424.      */  
  425.     @ViewDebug.ExportedProperty  
  426.     public synchronized int getMax() {  
  427.         return mMax;  
  428.     }  
  429.   
  430.     /**  
  431.      * <p>  
  432.      * Set the range of the progress bar to 0...<tt>max</tt>.  
  433.      * </p>  
  434.      *   
  435.      * @param max  
  436.      *            the upper range of this progress bar  
  437.      *   
  438.      * @see #getMax()  
  439.      * @see #setProgress(int)  
  440.      * @see #setSecondaryProgress(int)  
  441.      */  
  442.     public synchronized void setMax(int max) {  
  443.         if (max < 0) {  
  444.             max = 0;  
  445.         }  
  446.         if (max != mMax) {  
  447.             mMax = max;  
  448.             postInvalidate();  
  449.   
  450.             if (mProgress > max) {  
  451.                 mProgress = max;  
  452.                 refreshProgress(android.R.id.progress, mProgress, false);  
  453.             }  
  454.         }  
  455.     }  
  456.   
  457.     /**  
  458.      * <p>  
  459.      * Increase the progress bar's progress by the specified amount.  
  460.      * </p>  
  461.      *   
  462.      * @param diff  
  463.      *            the amount by which the progress must be increased  
  464.      *   
  465.      * @see #setProgress(int)  
  466.      */  
  467.     public synchronized final void incrementProgressBy(int diff) {  
  468.         setProgress(mProgress + diff);  
  469.     }  
  470.   
  471.     /**  
  472.      * <p>  
  473.      * Increase the progress bar's secondary progress by the specified amount.  
  474.      * </p>  
  475.      *   
  476.      * @param diff  
  477.      *            the amount by which the secondary progress must be increased  
  478.      *   
  479.      * @see #setSecondaryProgress(int)  
  480.      */  
  481.     public synchronized final void incrementSecondaryProgressBy(int diff) {  
  482.         setSecondaryProgress(mSecondaryProgress + diff);  
  483.     }  
  484.   
  485.     @Override  
  486.     public void setVisibility(int v) {  
  487.         if (getVisibility() != v) {  
  488.             super.setVisibility(v);  
  489.         }  
  490.     }  
  491.   
  492.     @Override  
  493.     public void invalidateDrawable(Drawable dr) {  
  494.         if (!mInDrawing) {  
  495.             if (verifyDrawable(dr)) {  
  496.                 final Rect dirty = dr.getBounds();  
  497.                 final int scrollX = mScrollX + mPaddingLeft;  
  498.                 final int scrollY = mScrollY + mPaddingTop;  
  499.   
  500.                 invalidate(dirty.left + scrollX, dirty.top + scrollY,  
  501.                         dirty.right + scrollX, dirty.bottom + scrollY);  
  502.             } else {  
  503.                 super.invalidateDrawable(dr);  
  504.             }  
  505.         }  
  506.     }  
  507.   
  508.     @Override  
  509.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  510.         // onDraw will translate the canvas so we draw starting at 0,0  
  511.         int right = w - mPaddingRight - mPaddingLeft;  
  512.         int bottom = h - mPaddingBottom - mPaddingTop;  
  513.   
  514.         if (mProgressDrawable != null) {  
  515.             mProgressDrawable.setBounds(0, 0, right, bottom);  
  516.         }  
  517.     }  
  518.   
  519.     @Override  
  520.     protected synchronized void onDraw(Canvas canvas) {  
  521.         super.onDraw(canvas);  
  522.   
  523.         Drawable d = mCurrentDrawable;  
  524.         if (d != null) {  
  525.             // Translate canvas so a indeterminate circular progress bar with  
  526.             // padding  
  527.             // rotates properly in its animation  
  528.             canvas.save();  
  529.             canvas.translate(mPaddingLeft, mPaddingTop);  
  530.             d.draw(canvas);  
  531.             canvas.restore();  
  532.         }  
  533.     }  
  534.   
  535.     @Override  
  536.     protected synchronized void onMeasure(int widthMeasureSpec,  
  537.             int heightMeasureSpec) {  
  538.         Drawable d = mCurrentDrawable;  
  539.   
  540.         int dw = 0;  
  541.         int dh = 0;  
  542.         if (d != null) {  
  543.             dw = Math  
  544.                     .max(mMinWidth, Math.min(mMaxWidth, d.getIntrinsicWidth()));  
  545.             dh = Math.max(mMinHeight, Math.min(mMaxHeight, d  
  546.                     .getIntrinsicHeight()));  
  547.         }  
  548.         dw += mPaddingLeft + mPaddingRight;  
  549.         dh += mPaddingTop + mPaddingBottom;  
  550.   
  551.         setMeasuredDimension(resolveSize(dw, widthMeasureSpec), resolveSize(dh,  
  552.                 heightMeasureSpec));  
  553.     }  
  554.   
  555.     @Override  
  556.     protected void drawableStateChanged() {  
  557.         super.drawableStateChanged();  
  558.   
  559.         int[] state = getDrawableState();  
  560.   
  561.         if (mProgressDrawable != null && mProgressDrawable.isStateful()) {  
  562.             mProgressDrawable.setState(state);  
  563.         }  
  564.     }  
  565.   
  566.     static class SavedState extends BaseSavedState {  
  567.         int progress;  
  568.         int secondaryProgress;  
  569.   
  570.         /**  
  571.          * Constructor called from {@link ProgressBar#onSaveInstanceState()}  
  572.          */  
  573.         SavedState(Parcelable superState) {  
  574.             super(superState);  
  575.         }  
  576.   
  577.         /**  
  578.          * Constructor called from {@link #CREATOR}  
  579.          */  
  580.         private SavedState(Parcel in) {  
  581.             super(in);  
  582.             progress = in.readInt();  
  583.             secondaryProgress = in.readInt();  
  584.         }  
  585.   
  586.         @Override  
  587.         public void writeToParcel(Parcel out, int flags) {  
  588.             super.writeToParcel(out, flags);  
  589.             out.writeInt(progress);  
  590.             out.writeInt(secondaryProgress);  
  591.         }  
  592.   
  593.         public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {  
  594.             public SavedState createFromParcel(Parcel in) {  
  595.                 return new SavedState(in);  
  596.             }  
  597.   
  598.             public SavedState[] newArray(int size) {  
  599.                 return new SavedState[size];  
  600.             }  
  601.         };  
  602.     }  
  603.   
  604.     @Override  
  605.     public Parcelable onSaveInstanceState() {  
  606.         // Force our ancestor class to save its state  
  607.         Parcelable superState = super.onSaveInstanceState();  
  608.         SavedState ss = new SavedState(superState);  
  609.   
  610.         ss.progress = mProgress;  
  611.         ss.secondaryProgress = mSecondaryProgress;  
  612.   
  613.         return ss;  
  614.     }  
  615.   
  616.     @Override  
  617.     public void onRestoreInstanceState(Parcelable state) {  
  618.         SavedState ss = (SavedState) state;  
  619.         super.onRestoreInstanceState(ss.getSuperState());  
  620.   
  621.         setProgress(ss.progress);  
  622.         setSecondaryProgress(ss.secondaryProgress);  
  623.     }  
  624. }  

方式三
   还是从源码入手,找到设置ProgressBar样式的progress_horizontal.xml文件,
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  <item android:id="@android:id/progress">  
  2.          <clip>  
  3.             <shape>  
  4.                  <corners android:radius="5dip" />  
  5.                  <gradient  
  6.                         android:startColor="#ffffd300"  
  7.                          android:centerColor="#ffffb600"  
  8.                         android:centerY="0.75"  
  9.                         android:endColor="#ffffcb00"  
  10.                         android:angle="270"  
  11.                  />  
  12.             </shape>  
  13.          </clip>  
  14.      </item>  
  为什么shape外面要包一层clip呢,官方文档解释是clipdrawable是可以自我复制的,来看看定义
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  <?xml version="1.0" encoding="utf-8"?>  
  2. <clip  
  3.      xmlns:android="http://schemas.android.com/apk/res/android"  
  4.      android:drawable="@drawable/drawable_resource"  
  5.      android:clipOrientation=["horizontal" | "vertical"]  
  6.     android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |  
  7.                      "fill_vertical" | "center_horizontal" | "fill_horizontal" |  
  8.                      "center" | "fill" | "clip_vertical" | "clip_horizontal"] />  
  android:clipOrientation有两个属性,默认为horizontal,android:gravity有两个属性,默认为left。那我们试试改成vertical和bottom会有什么效果,新建一个progress_vertical.xml,把源码progress_horizontal.xml的内容复制过来,这里去掉了secondaryProgress,修改了clip,shape的渐变中心centerY改为centerX
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  <item android:id="@android:id/progress">  
  2.          <clip  
  3.              android:clipOrientation="vertical"  
  4.              android:gravity = "bottom">  
  5.              <shape>  
  6.                  <corners android:radius="5dip" />  
  7.                <gradient  
  8.                          android:startColor="#ffffd300"  
  9.                         android:centerColor="#ffffb600"  
  10.                          android:centerX="0.75"  
  11.                          android:endColor="#ffffcb00"  
  12.                          android:angle="90"  
  13.                  />  
  14.             </shape>  
  15.         </clip>  
  16.      </item>  
  布局中android:progressDrawable="@drawable/progress_vertical",ok,搞定。
  代码下载(方式一代码)

  推荐博文: Android Canvas编程:对rotate()和translate()两个方法的研究



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值