Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android自定義View實現圖片上傳進度顯示(仿手機QQ上傳效果)

android自定義View實現圖片上傳進度顯示(仿手機QQ上傳效果)

編輯:關於Android編程

首先看下我們想要實現的效果如下圖(qq聊天中發送圖片時的效果):

\

再看一下我實現的效果:

\

 

1、效果已經看見了,下面我們來實現它。首先我創建一個android工程ProgressImageView。然後我們重寫ImageView控件,創建ProcessImageView類代碼如下:

 

package com.example.processimageview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

public class ProcessImageView extends ImageView {

    private Paint mPaint;// 畫筆
    int width = 0;
    int height = 0;
    Context context = null;
    int progress = 0;

    public ProcessImageView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public ProcessImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProcessImageView(Context context, AttributeSet attrs,
            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        mPaint = new Paint();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setAntiAlias(true); // 消除鋸齒
        mPaint.setStyle(Paint.Style.FILL);
        

        mPaint.setColor(Color.parseColor("#70000000"));// 半透明
        canvas.drawRect(0, 0, getWidth(), getHeight()- getHeight() * progress
                / 100, mPaint);

        mPaint.setColor(Color.parseColor("#00000000"));// 全透明
        canvas.drawRect(0, getHeight() - getHeight() * progress / 100,
                getWidth(), getHeight(), mPaint);

        mPaint.setTextSize(30);
        mPaint.setColor(Color.parseColor("#FFFFFF"));
        mPaint.setStrokeWidth(2);
        Rect rect = new Rect();
        mPaint.getTextBounds("100%", 0, "100%".length(), rect);// 確定文字的寬度
        canvas.drawText(progress + "%", getWidth() / 2 - rect.width() / 2,
                getHeight() / 2, mPaint);

    }

    public void setProgress(int progress) {
        this.progress = progress;
        postInvalidate();
    };

}


 

2、將ProcessImageView控件加入activity_layout.xml布局文件中,代碼如下;

 

 

    
   


 

 

3、最後一步,顯示效果(是不是很激動大笑),在MainActivity類加入顯示進度條的ImageView,代碼如下:

 

package com.example.processimageview;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Toast;

public class MainActivity extends Activity {

    ProcessImageView processImageView =null;
    private final int SUCCESS=0;
    int progress=0;
    
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
            case SUCCESS:
                Toast.makeText(MainActivity.this, "圖片上傳完成", Toast.LENGTH_SHORT).show();
                processImageView.setVisibility(View.GONE);
                break;
            }
        }
    };
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

      processImageView=(ProcessImageView) findViewById(R.id.image);
        //模擬圖片上傳進度
        new Thread(new Runnable() {
            @Override
            public void run() {
                 while (true){  
                     if(progress==100){//圖片上傳完成
                         handler.sendEmptyMessage(SUCCESS);
                         return;
                     }
                     progress++;
                     processImageView.setProgress(progress);
                    try{  
                        Thread.sleep(200);  //暫停0.2秒
                    } catch (InterruptedException e){  
                        e.printStackTrace();  
                    }  
                }  
            }
        }).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}



4、編譯運行,大功告成。

 

 

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved