2 pigeek PiGeek 于 2016.04.25 20:53 提问

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

这是自定义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)

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

3个回答

danielinbiti
danielinbiti   Ds   Rxr 2016.04.25 21:09

onDraw的代码呢?

PiGeek
PiGeek 谢谢你,我学习一下~
一年多之前 回复
danielinbiti
danielinbiti 回复PiGeek: activity和service的几种交互方式 http://blog.csdn.net/zhang31jian/article/details/7961506
一年多之前 回复
PiGeek
PiGeek ResultView resultView = new ResultView()是在BroadcastReceiver里面创建的,我一想可能真不是同一个。可是该怎么改呢
一年多之前 回复
danielinbiti
danielinbiti 回复PiGeek: 如果是service中ResultView resultView = new ResultView();和你前端显示的不是同一个吧
一年多之前 回复
PiGeek
PiGeek 不行哦,还是老样子。我是在service里面向BroadcastReceiver发送消息,BroadcastReceiver捕捉到后把相关数据通过countPerDay传递给View
一年多之前 回复
danielinbiti
danielinbiti 回复PiGeek: 嗯,看到postInvalidate了,你应该是在其他thread中调用该方法的?加上volatile ,同步线程变量 private volatile int[] countTimes = new int[7];
一年多之前 回复
PiGeek
PiGeek 回复danielinbiti: 有的哦,数值变化之后也会刷新一次view。但是得到的数值也是0
一年多之前 回复
danielinbiti
danielinbiti 回复PiGeek: 不是,调用countPerDay后,有界面刷新之类,使得onDraw调用到嘛?还是说就是在界面初始化的时候调用了一次onDraw
一年多之前 回复
PiGeek
PiGeek 回复danielinbiti: 调用resultView.countPerDay(drawCountTimes)是在另外一个onReceive里面调用的,在这里面打印drawCountTimes的数值也是正常的
一年多之前 回复
danielinbiti
danielinbiti 回复PiGeek: 调用resultView.countPerDay(drawCountTimes);这句后有onDraw内容的打印吗?
一年多之前 回复
PiGeek
PiGeek 已经贴上了哦,需要指点的地方也加上##号了~
一年多之前 回复
danielinbiti
danielinbiti 回复PiGeek: 你得把具体代码贴出来,照上面这么写,且顺序也没错,那肯定不会出错
一年多之前 回复
PiGeek
PiGeek 就是传过来的数组数据只在countPerDay这个方法里面是被赋值了的,在其他地方全部为空,不管是onDraw或是其他什么地方
一年多之前 回复
oSanYeCao1234567
oSanYeCao1234567   Ds   Rxr 2016.09.15 19:15

额,代码也太长啦吧,你可以简单说下问题

baidu_23086307
baidu_23086307   2016.04.26 06:51

传递自定义什么嘛vv

PiGeek
PiGeek 啊?就是通过countPerDay传入int[]数组呗
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!