0

在这里,我有一个自定义视图,我想在单击停止按钮时保存所有值(例如 rightx、topx、bottomx、leftx、i 等)。停止按钮位于添加了自定义视图的活动中。自定义视图:

     public GameView(Context context,AttributeSet attrs ) {

    super(context,attrs);

    leftx=0;
    topy=60;

    bottomy=120;

    z= String.valueOf(Character.toChars(i));

    cPaint = new Paint();
    cPaint.setColor(Color.RED);


    strt=45;
    arc=315;

    this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


    // cPaint.setStrokeWidth(2);

    tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    tPaint.setColor(Color.BLACK);
    float scale = getResources().getDisplayMetrics().scaledDensity;
    tPaint.setTextSize(40 * scale);

   }


    public void start()
    {
        flag6=true;
        if (this.flag2==false)
            new DrawThread(this);
    }

    public void stop()
    {
        //GameView gv=new GameView(this,x);

        this.flag2=false;
    }
    public void play(int resId)
    {

        Context context=this.getContext();
        stopplay(context);
        mp = MediaPlayer.create(context,resId);
        mp.setLooping(false);
        mp.start();
    }


    public void stopplay(Context context)
    {
        if(mp!=null)
        {
        mp.stop();
        mp.release();
        mp=null;
        }
    }
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


        parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        parentheight = MeasureSpec.getSize(heightMeasureSpec);
        this.setMeasuredDimension(
                parentWidth, parentheight);
        float a = parentWidth*0.25f;
        this.temp=(int)(a);
        this.temp1=parentWidth-temp;

        this.rightx = (int) (a);
        f1=a+30;
        this.n1= (int) (f1);
      //  this.n1=this.n1+30;
        f2=parentheight*0.25f+60;
        this.n2=(int)(f2);
       // Log.d("check","n1= "+n1);
       // Log.d("check","n2= "+n2);

        }


 @Override
   protected void onDraw(Canvas canvas) {
    // Drawing commands go here      
    oval =new RectF(leftx,topy,rightx,bottomy);

    canvas.drawArc(oval, strt, arc, true, cPaint);

     while(i<=90)
     {

     canvas.drawText(String.valueOf(Character.toChars(i)),f1,f2, tPaint);

     break;
     }
     Log.d("check","top= "+topy);
     Log.d("check","bottom= "+bottomy);
     Log.d("check","right= "+rightx);
     Log.d("check","left= "+leftx);      

     if(i>=91)
     {  
         canvas.drawText("Congratulations!!!\n",0,parentheight/2,tPaint);
     }

 }
 public boolean onTouchEvent(MotionEvent event)
 {
     float eventX = event.getX();
     float eventY = event.getY();
     switch(event.getAction()){
     case MotionEvent.ACTION_DOWN:
         dt.begin(eventX,eventY);
         return true;
     case MotionEvent.ACTION_MOVE:
         dt.moving();
         return true;

     }
     return true;
 }

保存值后,我想在按下继续按钮时再次加载值。我可以使用 setpreferences 方法还是有其他方法?

4

0 回答 0