欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 自定义view实现进度条加载效果实例代码

程序员文章站 2023-12-10 13:51:22
  这个其实很简单,思路是这样的,就是拿view的宽度,除以点的点的宽度+二个点 之间的间距,就可以算出大概能画出几个点出来,然后就通过canvas画出...

  这个其实很简单,思路是这样的,就是拿view的宽度,除以点的点的宽度+二个点 之间的间距,就可以算出大概能画出几个点出来,然后就通过canvas画出点,再然后就是每隔多少时间把上面移动的点不断的去改变它的坐标就可以,

效果如下:

Android 自定义view实现进度条加载效果实例代码

分析图:

Android 自定义view实现进度条加载效果实例代码

代码如下:

package com.example.dotloadview;
import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.canvas;
import android.graphics.paint;
import android.os.handler;
import android.util.attributeset;
import android.view.view;
/**
 * created by zhouguizhi on 6/18/15.
 */
public class dotloadprogressview extends view {
  private boolean isstart = false;
  private int dotindex =0;//点下面的角标
  private int dotnumber = 0;
  private paint mpaint;
  private int paddingtop = 50;
  private int paddingbottom = 50;
  private int paddingleft = 30;
  private int paddingright = 30;
  private handler mhandler = new handler();
  private long delay_time = 300;
  private bitmap bitmap;
  private int itemdotspace = 10;//二个点之间的距离
  private bitmap movebitmap ;
  private runnable mrunnable = new runnable() {
    @override
    public void run() {
      if (dotnumber != 0){
        dotindex = (dotindex + 1) % dotnumber;
      }
      invalidate();
      mhandler.postdelayed(mrunnable, delay_time);
    }
  };
  public dotloadprogressview(context context, attributeset attrs) {
    super(context, attrs);
    movebitmap = bitmapfactory.decoderesource(getresources(),r.drawable.active_dot);
    init();
  }
  private void init() {
    mpaint = new paint(paint.anti_alias_flag);
    mpaint.setstyle(paint.style.fill);
    mpaint.setdither(true);
    mpaint.setantialias(true);
  }
  @override
  protected void ondraw(canvas canvas) {
    super.ondraw(canvas);
    for(int i=0;i<dotnumber;i++){
      canvas.drawbitmap(bitmap,paddingleft+i*itemdotspace+bitmap.getwidth()*i,paddingtop,mpaint);
    }
    if(isstart){
      canvas.drawbitmap(movebitmap,paddingleft+dotindex*itemdotspace+movebitmap.getwidth()*dotindex,paddingtop,mpaint);
    }
  }
  @override
  protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
    int parentwidth = measurespec.getsize(widthmeasurespec);
    bitmap = bitmapfactory.decoderesource(getresources(),r.drawable.inactive_dot);
    setmeasureddimension(parentwidth,bitmap.getheight()+paddingtop+paddingbottom);
    int widthwithoutpadding = parentwidth - paddingleft - paddingright;
    dotnumber = calculatedotsnumber(widthwithoutpadding);
  }
  private int calculatedotsnumber(int width) {
    int number = (width / (itemdotspace + bitmap.getwidth()));
    return number;
  }
  public void startprogress() {
    isstart = true;
    dotindex = -1;
    mhandler.removecallbacks(mrunnable);
    mhandler.post(mrunnable);
  }
  public void stopprogress() {
    isstart = false;
    mhandler.removecallbacks(mrunnable);
    invalidate();
  }
  @override
  protected void onattachedtowindow() {
    super.onattachedtowindow();
    startprogress();
  }
  @override
  protected void ondetachedfromwindow() {
    super.ondetachedfromwindow();
    stopprogress();
  }
}

布局代码:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <com.example.dotloadview.dotloadprogressview
    android:id="@+id/progress1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
</relativelayout>

二个点其实是图片,在网上别人项目中拿出来用的,如果感觉那个点图片大了的话,有二个办法,第一叫你们美工切小点,第二就是通过drawable.setbound()去限制他大小了,我在这其实是偷懒,没有那么做,当然这点你也可以通过shade去做,而不用图片的话
最终的效果图还是这样的:

Android 自定义view实现进度条加载效果实例代码

总结

以上所述是小编给大家介绍的android 自定义view实现进度条加载效果实例代码,希望对大家有所帮助