Android 이미지 불 러 오기 진행 알림 실현

본 논문 의 사례 는 안 드 로 이 드 가 이미지 로드 진도 알림 을 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
먼저 위의 그림:

실현 원리:
첫 번 째 컨트롤 의 실현 원 리 는 ImageView 의 onDraw()방법 을 재 작성 하고 Canvas 의 clipRect()방법 으로 그림 의 표시 영역 을 제어 하 며 메 인 키 로 그림 의 표시 영역 을 확대 하여 점점 증가 하 는 효 과 를 실현 하 는 것 이다.
키 코드:

public class LoadingImageView extends ImageView {
 /***      */
 private Drawable bgDrawable;
 /**    */
 private Drawable fgDrawable;
 /**         */
 private boolean isShowProgress;
 
 private Resources rsc;
 private int progress;
 private int progressHeight;
 private int progressLeft;
 private int progressTop;
 private int progressRight;
 private int progressBottom;
 
 
 public LoadingImageView(Context context) {
 this(context,null);
 }
 
 public LoadingImageView(Context context, AttributeSet attrs) {
 this(context, attrs,0);
 }
 
 public LoadingImageView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 rsc = getResources();
 }
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 if(bgDrawable==null){
  return;
 }
 progressLeft = getMeasuredWidth()/2-(fgDrawable.getIntrinsicWidth()/2);
 progressTop = getMeasuredHeight()/2-(fgDrawable.getIntrinsicHeight()/2);
 progressRight = getMeasuredWidth()/2+(fgDrawable.getIntrinsicWidth()/2);
 progressBottom = getMeasuredHeight()/2+(fgDrawable.getIntrinsicHeight()/2);
 }
 
 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 return super.onTouchEvent(event);
 }
 
 /**
 *       
 * @param drawableRes
 */
 public void setBgDrawableRes(int drawableRes){
 bgDrawable = rsc.getDrawable(drawableRes);
 invalidate();
 }
 
 public void setFgDrawableRes(int drawableRes){
 fgDrawable = rsc.getDrawable(drawableRes);
 invalidate();
 }

 
 public void setProgress(int progress,boolean flag) {
 isShowProgress = flag;
 if(progress>=0&progress<=100){
  this.progress = progress;
  invalidate();
 }
 }
 
 
 @Override
 protected void onDraw(Canvas canvas) {
 if(bgDrawable!=null){
  bgDrawable.setBounds(progressLeft, progressTop, progressRight, progressBottom);
  bgDrawable.draw(canvas);
 }
 super.onDraw(canvas);
 if(bgDrawable!=null&&isShowProgress){
  bgDrawable.setBounds(progressLeft, progressTop, progressRight, progressBottom);
  bgDrawable.draw(canvas);
 }
 if(fgDrawable!=null&&isShowProgress){
  //              
  progressHeight = fgDrawable.getIntrinsicHeight()*progress/100;
  //    ,         
  canvas.clipRect(progressLeft,progressBottom-progressHeight,progressRight,progressBottom);
  fgDrawable.setBounds(progressLeft, progressTop, progressRight, progressBottom);
  fgDrawable.draw(canvas);
 }
 }
 
 
}
두 번 째 원형 로드 진도 의 원리 도 간단 하 다.바로 포물선 을 그 리 는 것 이다.포물선 의 각 도 를 계속 증가 시 켜 진 도 를 바 꾸 는 기능 을 실현 하 는 것 이다.
키 코드:

public class LoadingCircleView extends View {
 
 private final Paint paint; 
  private final Context context; 
  private Resources res;
  private int progress;
  private int ringWidth;
  //     
  private int ringColor;
 //     
  private int progressColor;
  //    
  private int textColor;
  //    
  private int textSize;
  
  private String textProgress;
  
 public LoadingCircleView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 this.context = context; 
 this.paint = new Paint(); 
 this.res = context.getResources();
    this.paint.setAntiAlias(true); //     
    this.ringWidth = dip2px(context, 10); //       
    this.ringColor = Color.rgb(233, 233, 233);
    this.progressColor = Color.rgb(146, 206, 108);
    this.textColor = Color.rgb(203, 203, 203);
    this.textSize = 30;
 }
 
 public LoadingCircleView(Context context, AttributeSet attrs) {
 this(context, attrs,0);
 }
 
 public LoadingCircleView(Context context) {
 this(context,null);
 }
 /**
 *       ,     0~100  
 * @param progress
 */
 public void setProgress(int progress) {
 if(progress>=0&&progress<=100){
 this.progress = progress;
 invalidate();
 }
 }
 /**
 *        
 * @param ringColor
 */
 public void setRingColor(int ringColor) {
 this.ringColor = res.getColor(ringColor);
 }
 /**
 *        
 * @param progressColor
 */
 public void setProgressColor(int progressColor) {
 this.progressColor = res.getColor(progressColor);
 }
 /**
 *       
 * @param textColor
 */
 public void setTextColor(int textColor) {
 this.textColor = res.getColor(textColor);
 }
 /**
 *       
 * @param textSize
 */
 public void setTextSize(int textSize) {
 this.textSize = textSize;
 }
 /**
 *       
 * @param ringWidth
 */
 public void setRingWidthDip(int ringWidth) {
 this.ringWidth = dip2px(context, ringWidth);
 }
 /**
 *              ,      
 */
 @Override
 protected void onDraw(Canvas canvas) {
 int center = getWidth()/2; 
    int radios = center-ringWidth/2;
     
     
    //     
    this.paint.setStyle(Paint.Style.STROKE); //       
    this.paint.setColor(ringColor);
    this.paint.setStrokeWidth(ringWidth); 
    canvas.drawCircle(center,center, radios, this.paint); 
    RectF oval = new RectF(center-radios, center-radios, center+radios, center+radios);
    this.paint.setColor(progressColor);
    canvas.drawArc(oval, 90, 360*progress/100, false, paint);
    this.paint.setStyle(Paint.Style.FILL);
    this.paint.setColor(textColor);
    this.paint.setStrokeWidth(0);
    this.paint.setTextSize(textSize);
    this.paint.setTypeface(Typeface.DEFAULT_BOLD);
    textProgress = progress+"%";
    float textWidth = paint.measureText(textProgress);
    canvas.drawText(textProgress, center-textWidth/2, center+textSize/2, paint);
     
     
    super.onDraw(canvas); 
 }
 
  /** 
   *           dp         px(  ) 
   */ 
  public static int dip2px(Context context, float dpValue) { 
    final float scale = context.getResources().getDisplayMetrics().density; 
    return (int) (dpValue * scale + 0.5f); 
  } }
컨트롤 이 정의 되면 Xml 에서 호출 할 수 있 습 니 다:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
   >
   <com.example.imagetest.LoadingImageView
     android:id="@+id/loading_image_view"
     android:layout_width="258px"
     android:layout_height="257px"
     android:background="#330000"
     >
   </com.example.imagetest.LoadingImageView>
   <com.example.imagetest.LoadingCircleView
     android:id="@+id/loading_cirle_view"
     android:layout_width="100dp"
     android:layout_height="100dp"
     >
   </com.example.imagetest.LoadingCircleView>
<!-- 
  <ListView 
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ></ListView> -->
 
</LinearLayout>
마지막 으로 사용 할 수 있 습 니 다.메 인 스 레 드 에서 로드 진 도 를 모 의 하고 스 레 드 를 시작 하 며 로드 진 도 를 모방 하여 점점 증가 합 니 다.

public class MainActivity extends Activity {
 
 ListView listview;
 private LoadingImageView loadingImageView;
 private LoadingCircleView loadingCircleView;
 
 private Handler handler = new Handler(){
 public void handleMessage(android.os.Message msg) {
  loadingImageView.setProgress(msg.what,true);
  loadingCircleView.setProgress(msg.what);
 };
 };
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 loadingImageView = (LoadingImageView) findViewById(R.id.loading_image_view);
 loadingImageView.setFgDrawableRes(R.drawable.bg_click_load_img);
 loadingImageView.setBgDrawableRes(R.drawable.ic_launcher);
 loadingImageView.setOnClickListener(new OnClickListener() {
  
  @Override
  public void onClick(View v) {
  loading(); 
  }
 });
 loadingCircleView = (LoadingCircleView) findViewById(R.id.loading_cirle_view);
 loadingCircleView.setOnClickListener(new OnClickListener() {
  
  @Override
  public void onClick(View v) {
  loading();
  }
 });
// listview = (ListView) findViewById(R.id.listview);
// showImage();
 }
 
 private void loading(){
 Thread t = new Thread(){
  @Override
  public void run() {
  int i = 0;
  while(i<=100){
   try {
    i++;
    handler.sendEmptyMessage(i);
    this.sleep(10);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
  super.run();
  }
 };
 t.start();
 }
 
 
 @Override
 protected void onResume() {
 super.onResume();
 }
 
 @Override
 protected void onPause() {
 super.onPause();
 }
 
 
 @Override
 protected void onDestroy() {
 super.onDestroy();
 }
 
 
}
자,대공 이 완성 되 었 으 니 운행 할 수 있 습 니 다.
자원 주소:Android 그림 로드 진행 알림
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기