android模拟支付宝芝麻信用(上)

本文介绍了一个自定义的Android视图组件,该组件能够绘制一个六边形评分图表,包括线条、文字和阴影区域。通过随机数生成不同长度的边来模拟评分条,适用于应用程序中的评分展示。

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

先来看一下支付宝的,这是看到别的感觉评分啥会用得到,然后写了一下


现在的话六角形是写死,阴影范围用的随机数,字也是自定义的,这个都可以放到构造方法,写成get,set方法,后期去改,

ok来看下view的代码:

package com.fanyafeng.orlitedemo.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.Random;

/**
 * Created by 365rili on 16/4/27.
 */
public class PolygonView extends View {
    //    竖线
    private Paint verPaint;
    //    横线
    private Paint porPaint;
    //    斜线
    private Paint slantPaint;
    //    写字
    private Paint textPaint;
    private static String[] listString = new String[]{"科技", "游戏", "音乐", "动画", "鬼畜", "影视"};


    public PolygonView(Context context) {
        super(context);
    }

    public PolygonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        verPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        porPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        slantPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    public PolygonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float strokeWidth = 2;
        verPaint.setAntiAlias(true);
        verPaint.setColor(Color.GREEN);
        verPaint.setStrokeWidth(strokeWidth);

        porPaint.setAntiAlias(true);
        porPaint.setColor(Color.GREEN);
        porPaint.setStrokeWidth(strokeWidth);

        slantPaint.setAntiAlias(true);
        slantPaint.setColor(Color.BLUE);
        slantPaint.setStrokeWidth(strokeWidth);
        slantPaint.setStyle(Paint.Style.FILL);

        textPaint.setTextSize(40);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setStrokeWidth(10);

        float startX = getLeft();
        float endX = getRight();
        float startY = getTop();
        float endY = getBottom();
        float width = getWidth();
        float height = getHeight();

        Random random = new Random();

//        圆心
        float circleX = width / 2;
        float circleY = height / 2;
//        半径
        float circleRadius = width > height ? (height - 180) / 2 : (width - 180) / 2;
//        外接圆
//        canvas.drawCircle(circleX, circleY, circleRadius, verPaint);

//        每一度所对应的数值
        double eachAngleLength = 2 * Math.PI / 360;
//        *eachAngleLenght相当于响应的度数
        float eachAngle = 60;
//        int[] xList = new int[]{203, 100, 380, 89, 345, 300, 20, 144};
        int[] xList = new int[]{random.nextInt(400), random.nextInt(400), random.nextInt(400), random.nextInt(400), random.nextInt(400), random.nextInt(400), random.nextInt(400), random.nextInt(400)};
        for (int i = 0; i < 6; i++) {
//            与外接圆交点的x,y值
            float finalPointX = (float) (circleX + circleRadius * Math.sin(eachAngle * eachAngleLength * i));
            float finalPointY = (float) (circleY + circleRadius * Math.cos(eachAngle * eachAngleLength * i));
//          画竖线
            canvas.drawLine(circleX, circleY, finalPointX, finalPointY, verPaint);

//            横线起点x,y
            float porPointStartX = (float) (circleRadius * Math.sin(eachAngle * eachAngleLength * i));
            float porPointStartY = (float) (circleRadius * Math.cos(eachAngle * eachAngleLength * i));
//            横线终点x,y
            float porPointEndX = (float) (circleRadius * Math.sin(eachAngle * eachAngleLength * (i + 1)));
            float porPointEndY = (float) (circleRadius * Math.cos(eachAngle * eachAngleLength * (i + 1)));
            for (int j = 1; j < 5; j++) {
//                画横线
                canvas.drawLine(circleX + porPointStartX / 4 * j, circleY + porPointStartY / 4 * j, circleX + porPointEndX / 4 * j, circleY + porPointEndY / 4 * j, porPaint);
            }

//            写字,字体的x,y
            float textPointX = (float) (circleX + (circleRadius + 60) * Math.sin(eachAngle * eachAngleLength * i));
            float textPointY = (float) (circleY + (circleRadius + 60) * Math.cos(eachAngle * eachAngleLength * i));
            canvas.drawText(listString[i], 0, listString[i].length(), textPointX, i == 3 ? textPointY + 30 : textPointY, textPaint);

            /**
             * 在这着重说一下阴影部分
             * 阴影的话应该是个数组,如果把这个view写成控件的话,就是每个项目占得百分比,但是数据需要搞对,ok现在模拟一下数据
             */
            Path path = new Path();
            path.moveTo(circleX, circleY);
            path.lineTo((float) (circleX + xList[i] * Math.sin(eachAngle * eachAngleLength * i)), (float) (circleY + xList[i] * Math.cos(eachAngle * eachAngleLength * i)));
            path.lineTo((float) (circleX + (i != 5 ? xList[i + 1] : xList[0]) * Math.sin(eachAngle * eachAngleLength * (i + 1))), (float) (circleY + (i != 5 ? xList[i + 1] : xList[0]) * Math.cos(eachAngle * eachAngleLength * (i + 1))));
            path.close();
            canvas.drawPath(path, slantPaint);
        }


    }
}


自定义View之仿支付宝芝麻信用仪表盘效果,喜欢的话,请给个star,谢谢.使用添加项目依赖Add it in your root build.gradle at the end of repositories: allprojects {         repositories { ... maven { url "https://jitpack.io" }         }     } Add the dependency     dependencies {             compile 'com.github.HotBitmapGG:CreditSesameRingView:V1.0.2' }新版芝麻信用使用     // The gradient color can define your own private final int[] mColors = new int[]{             0xFFFF80AB,             0xFFFF4081,             0xFFFF5177,             0xFFFF7997              }; // Set up the need to score mLayout = (RelativeLayout) view.findViewById(R.id.layout);       mButton = (Button) view.findViewById(R.id.btn);       newCreditSesameView = (NewCreditSesameView) view.findViewById(R.id.sesame_view);       mLayout.setBackgroundColor(mColors[0]);       mButton.setOnClickListener(new View.OnClickListener()       { @Override public void onClick(View view)            {                newCreditSesameView.setSesameValues(639);                startColorChangeAnim();            }       }); // The background color gradient animation Simply illustrates the effect Can customize according to your need public void startColorChangeAnim()      { ObjectAnimator animator = ObjectAnimator.ofInt(mLayout, "backgroundColor", mColors);          animator.setDuration(3000);          animator.setEvaluator(new ArgbEvaluator());          animator.start();      }旧版芝麻信用使用      // Set up the need to score oldCreditSesameView = (OldCreditSesameView) view.findViewById(R.id.sesame_view);       mButton = (Button) view.findViewById(R.id.btn);       mButton.setOnClickListener(new View.OnClickListener()       { @Override public void onClick(View view)           {               oldCreditSesameView.setSesameValues(639);           }       });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值