java-求解为何int[]数组在赋值后,数组元素还是为空

问题描述

求解为何int[]数组在赋值后,数组元素还是为空

这是自定义View里面的一个方法

 public void countPerDay(int[] newString){
        countTimes = newString;
        Log.d("TestInCountPerDay", String.valueOf(countTimes[0]) + ":" + String.valueOf(countTimes[4]) + ":" + String.valueOf(countTimes[5]) + ":" + String.valueOf(countTimes[6]));
        postInvalidate();
    }

在另一个类的onReceive调用这个方法,传入参数

 ResultView resultView = new ResultView();
 resultView.countPerDay(drawCountTimes);

View代码

public class ResultView extends View{

    private float mMinSize;
    private int mWidth;
    private int mHeight;
    private int mHeightUp;
    private int mBackgroundCorner;
    private int mStrokeWidth;
    private int mArcCenterX;
    private int mArcCenterY;

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    Calendar ca = Calendar.getInstance();

    private int[] days = new int[7];
# # private int[] countTimes = new int[7];

    private RectF mArcRect;

    private Paint mBackgroundPaint;
    private Paint mStrokePaint;
    private Paint mTextPaint;
    private Paint mLinePaint;
    private Paint mBarPaint;
    private SweepGradient mSweepGradient;

    private int[] mSteps;

    private String screenOffTimes = "";
    private String unlockTimes = "";

    public ResultView(Context context) {
        super(context, null);
    }

    public ResultView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ResultView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        init();
    }

    //接受从Async传来的数据,把值附给给countTime
    public void countTimes(String string,String string2){
        this.screenOffTimes = string;
        this.unlockTimes = string2;
        postInvalidate();
    }

##     public void countPerDay(int[] newString){
##         countTimes = newString;
##         Log.d("TestInCountPerDay", String.valueOf(countTimes[0]) + ":" + String.valueOf(countTimes[4]) + ":" + String.valueOf(countTimes[5]) + ":" + String.valueOf(countTimes[6]));
##         postInvalidate();
##     }

    public void init(){
        mBackgroundPaint = new Paint();
        mBackgroundPaint.setAntiAlias(true);
        mBackgroundPaint.setColor(Color.RED);
        mStrokePaint = new Paint();
        mStrokePaint.setAntiAlias(true);
        mStrokePaint.setStyle(Paint.Style.STROKE);
        mStrokePaint.setDither(true);                    // set the dither to true
        mStrokePaint.setStrokeJoin(Paint.Join.ROUND);    // set the join to round you want
        mStrokePaint.setStrokeCap(Paint.Cap.ROUND);      // set the paint cap to round too
        mStrokePaint.setPathEffect(new CornerPathEffect(10));   // set the path effect when they join.

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setColor(0xFF77838F);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setPathEffect(new DashPathEffect(new float[]{10, 3}, 0));

        mBarPaint = new Paint();
        mBarPaint.setAntiAlias(true);
        mBarPaint.setColor(0xFF63CFEC);
        mBarPaint.setStrokeCap(Paint.Cap.ROUND);

        mArcRect = new RectF();

        //柱状图的数值如下
        mSteps = new int[]{9000,12000,8900, 9200,7000,0,6000};

        ca.setTime(new Date());
        for (int i = 0;i<7;i++){
            ca.add(Calendar.DATE, -1);
            days[6-i]= Integer.parseInt(new SimpleDateFormat("dd").format(ca.getTime()));
        }

    }

    private void initSize(){
        mWidth = (int) (570*mMinSize);
        mHeight = (int) (650*mMinSize);
        mHeightUp = (int) (550*mMinSize);
        mBackgroundCorner = (int) (13*mMinSize);

        mStrokeWidth = (int) (20*mMinSize);

        mArcCenterX = mWidth/2;
        int arcRadius = (int) ((308 / 2) * mMinSize);
        mArcCenterY = (int) (200 * mMinSize);
        mArcRect.left = mArcCenterX - arcRadius;
        mArcRect.top = mArcCenterY - arcRadius;
        mArcRect.right = mArcCenterX + arcRadius;
        mArcRect.bottom = mArcCenterY + arcRadius;
        int[] colors = {0xFF9A9BF8,0xFF9AA2F7, 0xFF65CCD1,0xFF63D0CD,0xFF68CBD0,0xFF999AF6,0xFF9A9BF8};
        float[] positions = {0,1f/6,2f/6,3f/6,4f/6,5f/6,1};
        mSweepGradient = new SweepGradient(mArcCenterX, mArcCenterX, colors , positions);
    }

    @Override
    protected void onDraw(Canvas canvas) {

##         Log.d("TestInOnDraw", String.valueOf(countTimes[0]) + ":" + String.valueOf(countTimes[4]) + ":" + String.valueOf(countTimes[5]) + ":" + String.valueOf(countTimes[6]));

        // 1.draw background
        mBackgroundPaint.setColor(0xFF4C5A67);
        drawUpRoundRect(0, 0, mWidth, mHeightUp, mBackgroundCorner, mBackgroundPaint, canvas);
        mBackgroundPaint.setColor(0xFF496980);
        drawBelowRoundRect(0, mHeightUp, mWidth, mHeight, mBackgroundCorner, mBackgroundPaint, canvas);

        // 2.draw arc
        mStrokePaint.setStrokeWidth(mStrokeWidth);
        mStrokePaint.setShader(mSweepGradient);
        canvas.drawArc(mArcRect, -240, 300, false, mStrokePaint);
        //Intent intent = new Intent();

        //int a = Integer.parseInt(intent.getStringExtra("countTime"));
        // 3.draw text
        float xPos, yPos;
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(80 * mMinSize);
        yPos = mArcCenterY-((mTextPaint.descent() + mTextPaint.ascent()) / 2);
        //drawText直接显示数值
        canvas.drawText(screenOffTimes, mArcCenterX, yPos, mTextPaint);
        mTextPaint.setColor(0xFF828E98);
        mTextPaint.setTextSize(20 * mMinSize);
        yPos = 140*mMinSize -((mTextPaint.descent() + mTextPaint.ascent()) / 2);
        canvas.drawText("截至"+sdf.format(new Date())+"已点亮屏幕", mArcCenterX, yPos, mTextPaint);
        yPos = 260*mMinSize -((mTextPaint.descent() + mTextPaint.ascent()) / 2);
        canvas.drawText("其中解锁"+unlockTimes+"次", mArcCenterX, yPos, mTextPaint);
        mTextPaint.setColor(Color.WHITE);
        xPos = mArcCenterX - 40*mMinSize;
        yPos = 380 * mMinSize;
        canvas.drawText("第", xPos, yPos, mTextPaint);
        xPos = mArcCenterX + 40*mMinSize;
        canvas.drawText("名", xPos, yPos, mTextPaint);
        mTextPaint.setTextSize(30 * mMinSize);
        canvas.drawText("44", mArcCenterX, yPos, mTextPaint);

        mTextPaint.setTextSize(18*mMinSize);
        mTextPaint.setColor(0xFF77838F);
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        xPos = 30*mMinSize;
        yPos = 420*mMinSize;
        canvas.drawText("最近7天",xPos, yPos, mTextPaint);

        mTextPaint.setTextAlign(Paint.Align.RIGHT);
        xPos = 540 * mMinSize;
        yPos = 420*mMinSize;
        canvas.drawText("平均125次/天", xPos, yPos, mTextPaint);

        // 4. draw dash line
        xPos = 30 * mMinSize;
        yPos = 440 * mMinSize;
        mLinePaint.setStrokeWidth(2*mMinSize);
        canvas.drawLine(xPos, yPos, mWidth - xPos, yPos,mLinePaint);

        // 5. draw bars and date
        mBarPaint.setStrokeWidth(mStrokeWidth);
        float halfBarGap = 510 * mMinSize / 14;
        float maxLength = 440 * mMinSize;
        yPos = 480 * mMinSize;
        int maxBar = 0;
        for(int step : mSteps){
            if(step > maxBar){
                maxBar = step;
            }
        }
        float length;
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(18*mMinSize);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        for(int i = 0; i < mSteps.length; i++){
            xPos = (2*i+1)* halfBarGap + 30 *mMinSize;
            length = (yPos - maxLength) * mSteps[i]*1f/maxBar;
            canvas.drawLine(xPos, yPos, xPos, yPos -length, mBarPaint );
            //画出柱状图下面的日期
            String date = String.format("%02d日", days[i]);
            canvas.drawText(date,xPos, 520*mMinSize, mTextPaint );
        }

        // 6.draw text below
        mTextPaint.setTextAlign(Paint.Align.LEFT);
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(23 * mMinSize);
        canvas.drawText("这是被隐藏的内容", 30 * mMinSize, 610 * mMinSize, mTextPaint);
        mTextPaint.setTextAlign(Paint.Align.RIGHT);
        mTextPaint.setColor(0xFF63CFEC);
        canvas.drawText("查看  >", 540 * mMinSize, 610 * mMinSize, mTextPaint);

    }

    private void drawUpRoundRect(float left, float top, float right, float bottom, float radius, Paint paint, Canvas canvas) {
        Path path = new Path();
        path.moveTo(left, top);
        path.lineTo(right-radius, top);
        path.quadTo(right, top, right, top + radius);
        path.lineTo(right, bottom);
        path.lineTo(left, bottom);
        path.lineTo(left, top + radius);
        path.quadTo(left, top, left + radius, top);
        canvas.drawPath(path, paint);
    }
    private void drawBelowRoundRect(float left, float top, float right, float bottom, float radius, Paint paint, Canvas canvas) {
        Path path = new Path();
        path.moveTo(left, top);
        path.lineTo(right, top);
        path.lineTo(right, bottom-radius);
        path.quadTo(right, bottom,right-radius,bottom);
        path.lineTo(left + radius, bottom);
        path.quadTo(left,bottom,left,bottom-radius);
        path.lineTo(left, top);
        canvas.drawPath(path, paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int desiredWidth = Integer.MAX_VALUE;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        //Measure Width
        if (widthMode == MeasureSpec.EXACTLY) {
            //Must be this size
            width = widthSize;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            width = Math.min(desiredWidth, widthSize);
        } else {
            //Be whatever you want
            width = desiredWidth;
        }
        mMinSize = width/570.f;
        int desiredHeight = (int) (mMinSize*650);
        initSize();
        //Measure Height=
        if (heightMode == MeasureSpec.EXACTLY) {
            //Must be this size
            height = heightSize;
        } else if (heightMode == View.MeasureSpec.AT_MOST) {
            //Can't be bigger than...
            height = Math.min(desiredHeight, heightSize);
        } else {
            //Be whatever you want
            height = desiredHeight;
        }

        //MUST CALL THIS
        setMeasuredDimension(width, height);
    }

}

可是传递过来的数据在onDraw里面不生效,只在countPerDay里面生效(就是在countPerDay里面打log,数值都是正常的。但是在onDraw里面打log就全部都为0)

这是为何,本人初学小白,请大神们指教。

解决方案

onDraw的代码呢?

解决方案二:

传递自定义什么嘛vv

时间: 2024-12-28 05:57:17

java-求解为何int[]数组在赋值后,数组元素还是为空的相关文章

ie 11-JS里的数组对象赋值后是空

问题描述 JS里的数组对象赋值后是空 var obj_frm = new Array(); function SetFrom(obj) { obj_frm[obj.name] = obj; } 为什么了?浏览器是IE11 解决方案 js对象和数组JS对象和数组js中的数组对象 解决方案二: 说明方法体里的语句就没执行,你没有地方调用 解决方案三: obj是什么呀,alert一下看看是不是空,我觉得可能是obj_frm[obj.name] = obj;报错了 解决方案四: 你在方法里把要用的参数输

二维数组存储数据-如何定义一个二维数组和赋值,数组里面需要放【String】【List】

问题描述 如何定义一个二维数组和赋值,数组里面需要放[String][List] 我现在用多个线程处理一个List 每个线程取List里面的50条数据进行处理返回一个List.然后把线程名和返回的List放入二维数组里面. 解决方案 不用数组用map怎么样map.key=线程ID,val=处理数据集合,如果非要数组的话,你用object类型数组

C语言结构体数组同时赋值的另类用法

说到C语言结构体数组的同时赋值,许多人一想就会想到用以下的这种方法,咱们来写一个例子: #include <stdio.h> struct student { int a; int b ; int c ; }; struct student array1[1000] ; int main(void) { int i ; for(i = 0 ; i < 1000 ; i++) { array[i].a = 1 ; array[i].b = 2 ; array[i].c = 3 ; } fo

c语言-一个未满的整型数组,怎么求真实元素个数?

问题描述 一个未满的整型数组,怎么求真实元素个数? 比如一个整型数组:int a[10]; 这个数组没有存放满10个数.我怎么知道其中的数据元素个数? 解决方案 基本上不可能知道. 如果你一定想知道,可以试试给数组赋一个不会用到的初值,如:0xFFFFFFFF,或者 零. 然后对数组进行循环统计,看看哪些下标不再是初值,就是实际的元素.然后,统计一下个数即可. 解决方案二: 数组长度声明后每个元素有默认初始值,为0,没有所谓的存满吧(刚刚学习不知道对不对C#).如果你知道你的赋值都不为0,可以通

有关java二维数组的赋值

问题描述 有关java二维数组的赋值 有关二维数组的赋值 public class HelloWorld { public static void main(String[] args) { // 定义两行三列的二维数组并赋值 String [][] names = new String[2][3]; names = {{"tom","jack","mike"},{"zhangsan","lisi",&qu

java中数组的问题,int [3]表示有三个元素的类型是int的吗

问题描述 java中数组的问题,int [3]表示有三个元素的类型是int的吗 int [3]表示有三个元素的类型是int的吗 int [3] [ ] int [ ] [3] 有语法错误吗? int [3] [ ] ==int [ ] [3] 吗 解决方案 你刚学的java吧. int[3]没有这种写法 一般是int[] a =new int[3]代表的是一个数组. int [3] [ ]这种是二维数组.不写默认为0. int [3] [ ] ==int [ ] [3]哪有这种写法~ 解决方案二

初始化-C++ char数组不能赋值中文

问题描述 C++ char数组不能赋值中文 #includeusing namespace std;class student{public: char *getname() { cout << ""姓名:""; cin >> name[10]; //为什么输入字母.数字都可以正常运行,输入中文就不行 count++; return name; } void seteng() { cout << ""英语:&q

代码-JAVA写着写着就懵了,数组怎么删除里面的元素呢

问题描述 JAVA写着写着就懵了,数组怎么删除里面的元素呢 不能删除或者说赋值为null ,我该添加点什么代码进去才能实现呢,还要朝前移动一位怎么解决 我感觉我自己基础不行了,求大神指明一个学习方向 import java.util.*; import javax.swing.text.StyledEditorKit.ForegroundAction; public class dvdMgr { Scanner input=new Scanner(System.in); DVD[] dvd=ne

java编写一个算法,一个数用数组表示,执行加1操作,之后的数组用一个数表示。

问题描述 java编写一个算法,一个数用数组表示,执行加1操作,之后的数组用一个数表示. 用java编写一个算法,一个数用数组表示,执行加1操作,之后的数组用一个数表示. 解决方案 数组可以表示很多数的-你说,用一个数表示啥意思?? 解决方案二: 你应该是想要下面的实现.如果有用请采纳. import java.util.ArrayList; import java.util.List; public class TestMain { public static void main(String