//主页
package com.example.customcircleview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText edit_one,edit_two,edit_three;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
edit_one = findViewById(R.id.Edit_one);
edit_two = findViewById(R.id.Edit_two);
edit_three = findViewById(R.id.Edit_three);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Integer> list = new ArrayList<>();
int inputchart_one = Integer.parseInt(edit_one.getText().toString());
int inputchart_two = Integer.parseInt(edit_two.getText().toString());
int inputchart_three = Integer.parseInt(edit_three.getText().toString());
list.add(inputchart_one);
list.add(inputchart_two);
list.add(inputchart_three);
Intent intent = new Intent(MainActivity.this,ShowActivity.class);
intent.putIntegerArrayListExtra("list",list);
startActivity(intent);
}
});
}
}
//主页布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/Edit_one"
android:hint="请输入第一个矩形图的值"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/Edit_two"
android:hint="请输入第二个矩形图的值"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/Edit_three"
android:hint="请输入第三个矩形图的值"
/>
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/button"
android:text="点击"
android:textSize="23dp"
/>
</LinearLayout>
//点击跳转后跳到展示页面
package com.example.customcircleview;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
public class ShowActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
Intent intent = getIntent();
ArrayList<Integer> list = intent.getIntegerArrayListExtra("list");
int aa = list.get(0);
int bb = list.get(1);
int cc = list.get(2);
int[] num = new int[]{aa,bb,cc};
CustomView customView = findViewById(R.id.custom);
customView.setData(num);
}
}
//自定义一个VIew
package com.example.customcircleview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class CustomView extends View {
Paint mpaint;
private int[] top;
public CustomView(Context context) {
super(context);
init();
}
public CustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public void setData(int[] num){
int aa = num[0];
int bb = num[1];
int cc = num[2];
top = new int[]{aa,bb,cc};
}
public void init(){
mpaint = new Paint();
mpaint.setColor(Color.CYAN);
mpaint.setStrokeWidth(10);
mpaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//矩形
mpaint.setColor(Color.RED);
canvas.drawRect(50, 300 - top[0], 160, 300, mpaint);
mpaint.setColor(Color.BLACK);
canvas.drawRect(200, 300 - top[1], 330, 300, mpaint);
mpaint.setColor(Color.GREEN);
canvas.drawRect(370, 300 - top[2], 480, 300, mpaint);
//X轴
mpaint.setColor(Color.BLACK);
canvas.drawLine(10, 300, 700, 300, mpaint);
//Y轴
canvas.drawLine(10, 10, 10, 300, mpaint);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
}
//自定义View 的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ShowActivity">
<com.example.customcircleview.CustomView
android:id="@+id/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</android.support.constraint.ConstraintLayout>