一.简介:实现Button的样式改变,丰富Button点击效果的样式。
详情介绍和源码下载:https://github.com/dmytrodanylyk/android-process-button
二.效果:(这只是其中一种)
三.实现方式:
第一步:添加类库:下载地址:http://download.csdn.net/detail/u012372365/9624142
第二步:依赖工具类ProgressGenerator
package button.button_style_0907;
import android.os.Handler;
import com.dd.processbutton.ProcessButton;
import java.util.Random;
public class ProgressGenerator {
public interface OnCompleteListener {
public void onComplete();
}
private OnCompleteListener mListener;
private int mProgress;
public ProgressGenerator(OnCompleteListener listener) {
mListener = listener;
}
public void start(final ProcessButton button) {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mProgress += 10;
button.setProgress(mProgress);
if (mProgress < 100) {
handler.postDelayed(this, generateDelay());
} else {
mListener.onComplete();
}
}
}, generateDelay());
}
private Random random = new Random();
private int generateDelay() {
return random.nextInt(1000);
}
}
第三步:实现类
package button.button_style_0907;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.dd.processbutton.iml.ActionProcessButton;
public class MainActivity extends Activity implements ProgressGenerator.OnCompleteListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
//定义:ProgressGenerator
final ProgressGenerator progressGenerator = new ProgressGenerator(this);
final ActionProcessButton btnSignIn = (ActionProcessButton) findViewById(R.id.btnSignIn);
btnSignIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progressGenerator.start(btnSignIn);
btnSignIn.setEnabled(false);
}
});
}
public void onComplete() {
Toast.makeText(this, "开始结束......", Toast.LENGTH_LONG).show();
}
}
xml样式:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.dd.processbutton.iml.ActionProcessButton
android:id="@+id/btnSignIn"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:text="登录"
android:textColor="@android:color/white"
android:textSize="18sp"
custom:pb_colorComplete="@color/green_complete"
custom:pb_colorNormal="@color/blue_normal"
custom:pb_colorPressed="@color/blue_pressed"
custom:pb_colorProgress="@color/purple_progress"
custom:pb_textComplete="成功加载"
custom:pb_textProgress="开始加载" />
</LinearLayout>