自定义组合控件,控件上的按钮点击事件使用接口回调跳转到自定义阶梯矩阵页面,点击按钮进度条开始运行,运行100%以后跳转到扫描二维码页面,
先写自定义组合控件的布局LoginView 继承自LinearLayout
public class LoginViewextendsLinearLayoutimplementsView.OnClickListener{
private Context _context;
public LoginView(Context context) {
super(context);
}
public LoginView(Context context, AttributeSet attrs) {
super(context, attrs);
_context = context;
//将指定布局id填充到当前页面
View view = LayoutInflater.from(context).inflate(R.layout.view_login, this, true);
Button right_btn = view.findViewById(R.id.right_btn);
right_btn.setOnClickListener(this);
}
public LoginView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onClick(View view) {
/*if(view.getId()==R.id.right_btn){
Toast.makeText(_context,"right",Toast.LENGTH_SHORT).show();
}*/
//调用接口的方法
loginViewClickListener.loginViewButtonClicked(view);
}
//定义接口的成员
OnLoginViewClickListener loginViewClickListener;
//接口的setter
public void setOnLoginViewClickListenler(OnLoginViewClickListener loginViewClickListener){
//在setter中把这个接口的实现赋值给这个loginview的上面定义的接口
this.loginViewClickListener = loginViewClickListener;
}
//接口
public interface OnLoginViewClickListener{
public void loginViewButtonClicked(View v);//传的参数
}
}
写自定义组合控件LoginView 的对应的布局view_login
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#D4DBED"
android:orientation="horizontal">
<Button
android:textSize="23sp"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="#D4DBED"
android:text="<" />
<TextView
android:textSize="23sp"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#D4DBED"
android:text="那些花儿" />
<Button
android:id="@+id/right_btn"
android:textSize="23sp"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="#D4DBED"
android:text="⑤" />
</LinearLayout>
</LinearLayout>
activity_main 把自定义组合控件引入进来
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.example.a171104zhouliu_moniti.LoginView
android:id="@+id/loginView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
>
<com.example.a171104zhouliu_moniti.CustomProgressView
android:id="@+id/custom_ProgressView"
android:layout_width="200dp"
android:layout_height="200dp"
/>
<Button
android:id="@+id/saomiao"
android:text="扫描二维码"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
MainActivity里面的代码,拿到自定义控件的id,,new出接口,点击按钮执行进度条
public class MainActivity extends AppCompatActivity {
private LoginView loginView;
private Button btnsaomiao;
private CustomProgressView customProgressView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//拿到滚动条的视图
customProgressView = (CustomProgressView) findViewById(R.id.custom_ProgressView);
btnsaomiao = (Button) findViewById(R.id.saomiao);
loginView = (LoginView) findViewById(R.id.loginView);
//在这个实现上把接口new出来
loginView.setOnLoginViewClickListenler(new LoginView.OnLoginViewClickListener() {
@Override
public void loginViewButtonClicked(View v) {
//判断当前点击的v是哪个
if(v.getId()==R.id.right_btn){
// Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_LONG).show();
//点击右边的按钮 跳转到 secondActivity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
});
//扫描按钮的点击事件
btnsaomiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//点击按钮 调用滚动条的方法
customProgressView.start();
}
});
}
}
自定义圆和进度条CustomProgressView的代码
public class CustomProgressView extends View{
//定义画笔
private Paint paint;
//进度的值
private int progress = 0;//一开始是0
//控制循环的变量
private boolean running = true;
Context context;
public CustomProgressView(Context context) {
super(context);
}
public CustomProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
//创建画笔
paint = new Paint();
paint.setAntiAlias(true);//抗锯齿
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);//填充是空心的
}
//抽成一个方法,,
public void start(){
new Thread(new Runnable() {
@Override
public void run() {
//循环
while(running){
if(progress>=360){
//如果进度已经大于了360,就跳出
running = false;
Intent intent = new Intent(context, CaptureActivity.class);
context.startActivity(intent);
return;
}
progress+=10;//进度每次加10
//子线程刷新,
postInvalidate();
try {
//睡眠
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public CustomProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获取当前view 的宽度,x,y确定了圆心的位置
int x = getWidth()/2;
int y = getHeight()/2;
int radius = 100;//圆的半径
//利用这个rectf来确定弧形的范围
RectF rectF = new RectF(x-radius,y-radius,x+radius,y+radius);
paint.setColor(Color.GRAY);
paint.setStrokeWidth(20);
canvas.drawCircle(x,y,radius,paint);
//user center 为false不从中心点开始
paint.setColor(Color.GREEN);
paint.setStrokeWidth(20);
canvas.drawArc(rectF,-90,progress,false,paint);
//计算百分比的值,,整体强转成int
int zhi = (int) ((float) progress/360*100);
//计算百分比的值的位置
float zhiWidth = paint.measureText(zhi+"%");
//根据这个矩形 求 百分比值的 高度
Rect rectZhi = new Rect();
paint.getTextBounds(zhi+"%",0,(zhi+"%").length(),rectZhi);
//字符串的高度:rectZhi.height()
paint.setTextSize(15);
paint.setStrokeWidth(1);
//画文字
canvas.drawText(zhi+"%",x-zhiWidth/2,x+zhiWidth/2,paint);
}
}
点击组合控件上的按钮,跳转到secondAcitivity里面,,布局里面引入自定义阶梯矩阵的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.example.a171104zhouliu_moniti.LoginView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></com.example.a171104zhouliu_moniti.LoginView>
<com.example.a171104zhouliu_moniti.CustomJieTiRect
android:layout_width="match_parent"
android:layout_weight="5"
android:layout_height="0dp" />
</LinearLayout>
自定义阶梯矩阵的代码
public class CustomJieTiRect extends View{
public CustomJieTiRect(Context context) {
super(context);
}
public CustomJieTiRect(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomJieTiRect(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint1 = new Paint();
paint1.setTextSize(10);
paint1.setColor(Color.BLACK);
Rect rect = new Rect(0,0,250,80);
canvas.drawRect(rect,paint1);
Paint paint2 = new Paint();
paint2.setTextSize(10);
paint2.setColor(Color.RED);
Rect rect1 = new Rect(250,80,500,160);
canvas.drawRect(rect1,paint2);
Paint paint3 = new Paint();
paint3.setTextSize(10);
paint3.setColor(Color.BLUE);
Rect rect2 = new Rect(500,160,1000,250);
canvas.drawRect(rect2,paint3);
/* canvas.drawRect(0,0,150,80,paint1);
canvas.drawRect(150,80,300,160,paint2);
canvas.drawRect(300,160,500,250,paint3);*/
/*Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
canvas.drawRect(0,0,getWidth()/3,70,paint);
paint.setColor(Color.GRAY);
canvas.drawRect(getWidth()/3,70,getWidth()/2+70,140,paint);
paint.setColor(Color.RED);
canvas.drawRect(getWidth()/2+70,140,getWidth(),210,paint);*/
}}