android 自定义View实现进度条增长
2015-04-08 11:50
531 查看
现在的app下载的时候是下载的时候使用一个圆然后根据下载的进度进行显示:
自定义个atterx.xml文件:
然后进行layout.xml中调用:
在mainactivity中进行数据的调用:
自定义个atterx.xml文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RoundProgressBar"> <attr name="roundColor" format="color"/> <attr name="roundProgressColor" format="color"/> <attr name="roundWidth" format="dimension"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable> </resources>
然后进行layout.xml中调用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android_progress="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="START" /> <com.xushuangshuang.planshow.RoundProgressBar android:id="@+id/round_progress_bar" android:layout_width="60dp" android:layout_height="60dp" android_progress:roundColor="@color/skyblue" android_progress:roundWidth="10dip" android_progress:roundProgressColor="@color/red" /> </LinearLayout>然后在自定义一个view的实现类:
public class RoundProgressBar extends View { private int roundColor; private float roundWidth; private int roundProgressColour; private Paint paint; private int progress; public RoundProgressBar(Context context) { this(context, null); } public RoundProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint = new Paint(); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar); roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, R.color.skyblue); roundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5); roundProgressColour = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, R.color.red); typedArray.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int centre = getWidth() / 2; int radius = (int) (centre - roundWidth / 2); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(roundWidth); paint.setColor(roundColor); canvas.drawCircle(centre, centre, radius, paint); paint.setStrokeWidth(roundWidth); paint.setColor(roundProgressColour); RectF rectF = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); paint.setStyle(Paint.Style.STROKE); canvas.drawArc(rectF, 270, 360 * progress / 100, false, paint); } public synchronized int getProgress() { return progress; } public synchronized void setProgress(int progress) { if (progress < 0) { throw new IllegalArgumentException("progress not less than 0"); } if (progress > 100) { progress = 100; } if (progress <= 100) { this.progress = progress; <span style="color:#ff0000;">postInvalidate();//这句很重要,他表示了每一次进来后都需要进行重绘</span> } } }
在mainactivity中进行数据的调用:
public class MainActivity extends ActionBarActivity { RoundProgressBar progressBar; int progress = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.start); progressBar = (RoundProgressBar) findViewById(R.id.round_progress_bar); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { while (progress <= 100) { Log.e("test", progress + ""); progress += 3; progressBar.setProgress(progress); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } }); }
http://download.csdn.net/detail/xushuangshuang1/8573645这是源代码
相关文章推荐
- 自定义View实现Android圆形进度条,支持自定义显示的样式
- Android:自定义View实现水波进度效果
- Android自定义view实现水波纹进度球效果
- Android自定义View——使用贝塞尔曲线实现流量进度条
- android viewPager 实现图片无限循环滑动并带有进度的自定义布局
- Android 自定义View修炼-仿360手机卫士波浪球进度的实现
- Android自定义View实现钟摆效果进度条PendulumView
- android 自定义view实现数字进度条
- Android编程基于自定义View实现绚丽的圆形进度条功能示例
- Android自定义WaveView实现波浪进度效果
- Android:自定义View实现绚丽的圆形进度条
- Android自定义View之实现环形进度条
- Android 自定义View制作随时间增长的平滑进度条
- android 自定义view+属性动画实现充电进度条
- android自定义View实现图片上传进度显示(仿手机QQ上传效果)
- Android自定义View-绘制扇形实现圆形进度
- Android 自定义View修炼-仿360手机卫士波浪球进度的实现
- Android中使用自定义View实现下载进度的显示
- Android编程使用自定义View实现水波进度效果示例
- android 自定义view+属性动画实现充电进度条