自定义View 输入数据生成柱状图

本文介绍了一个自定义的Android图表绘制应用,通过用户输入三个不同数值,应用将这些数值转化为矩形图并显示在一个自定义的View中。文章详细展示了如何在Android应用中创建自定义View,包括设置布局、接收用户输入、解析数据以及使用Canvas进行图形绘制的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//主页

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值