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 그림 로드 진행 알림
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.