package com.jaiky.test.faceloadingview;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* Loading Faceview
* @author Jaiky
* @date Jul 4, 2016
* PS: Not easy to write code, please indicate.
*/
public class FaceImageView extends ImageView{
private AnimationDrawable loadingDrawable;
private AnimationDrawable failedDrawable;
public FaceImageView(Context context) {
super(context);
init();
}
public FaceImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FaceImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init(){
setImageResource(R.drawable.face_progressbar_default);
loadingDrawable = (AnimationDrawable) getDrawable();
failedDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.face_progressbar_failed);
loadingDrawable.start();
}
/**
* 设置载入失败
*/
public void setFailed() {
setImageDrawable(failedDrawable);
failedDrawable.start();
}
/**
* 设置正在载入
*/
public void setloading() {
setImageDrawable(loadingDrawable);
loadingDrawable.start();
}
public void startAnimation(){
AnimationDrawable anim = (AnimationDrawable) getDrawable();
anim.start();
}
public void stopAnimation(){
AnimationDrawable anim = (AnimationDrawable) getDrawable();
//停留在第一针
anim.setVisible(true, true);
anim.stop();
}
public void pauseAnimation(){
AnimationDrawable anim = (AnimationDrawable) getDrawable();
anim.stop();
}
}
package com.jaiky.test.faceloadingview;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
/**
* Loading Faceview
* @author Jaiky
* @date Jul 4, 2016
* PS: Not easy to write code, please indicate.
*/
public class FaceLoadingView extends FrameLayout{
private TextView tvInfo;
private FaceImageView faceView;
public FaceLoadingView(Context context) {
super(context);
init(context);
}
public FaceLoadingView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public FaceLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
public void init(Context context){
//设置动画视图
faceView = new FaceImageView(context);
DisplayMetrics dm = getResources().getDisplayMetrics();
//int widthHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 220, dm);
LayoutParams faceLayout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
faceLayout.gravity = Gravity.CENTER;
faceView.setLayoutParams(faceLayout);
//设置显示文本
tvInfo = new TextView(context);
LayoutParams tvLayout = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tvLayout.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
tvLayout.bottomMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, dm);
tvLayout.leftMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, dm);
tvInfo.setLayoutParams(tvLayout);
tvInfo.setText("努力加载中...");
tvInfo.setTextColor(Color.parseColor("#757575"));
tvInfo.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11);
//添加到布局
addView(faceView);
addView(tvInfo);
}
public void setFailed() {
setVisibility(View.VISIBLE);
faceView.setFailed();
tvInfo.setText("载入失败,请刷新尝试...");
}
public void setFailedWithMsg(String msg) {
setVisibility(View.VISIBLE);
faceView.setFailed();
tvInfo.setText(msg);
}
public void setloading() {
setVisibility(View.VISIBLE);
faceView.setloading();
tvInfo.setText("努力加载中...");
}
public void setloadingWithMsg(String msg) {
setVisibility(View.VISIBLE);
faceView.setloading();
tvInfo.setText(msg);
}
/**
* 隐藏视图
*/
public void hiddenView(){
setVisibility(View.GONE);
}
/**
* 显示视图
*/
public void show(){
setVisibility(View.VISIBLE);
}
/**
* 设置加载消息内容
*
* @param msg
*/
public void setMsg(String msg) {
tvInfo.setText(msg);
}
}
package com.jaiky.test.faceloadingview;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListActivity.class);
intent.putExtra("isFail", false);
startActivity(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ListActivity.class);
intent.putExtra("isFail", true);
startActivity(intent);
}
});
}
}
package com.jaiky.test.faceloadingview;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class ListActivity extends AppCompatActivity {
TextView mTextView;
FaceLoadingView mFaceLoadingView;
boolean isFail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
mFaceLoadingView = (FaceLoadingView) findViewById(R.id.faceView);
mTextView = (TextView) findViewById(R.id.mTextView);
mTextView.setVisibility(View.GONE);
isFail = getIntent().getBooleanExtra("isFail", false);
//Simulate get data
if (isFail) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mFaceLoadingView.setFailed();
}
}, 5000);
}
else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mFaceLoadingView.hiddenView();
mTextView.setVisibility(View.VISIBLE);
}
}, 5000);
}
}
}
欢迎转载,但请保留文章原始出处
作者:Jaiky_杰哥
出处:http://blog.csdn.net/jaikydota163/article/details/52098851