【问题标题】:Refresh SurfaceView-->Canvas on OnClickListener Android在 OnClickListener Android 上刷新 SurfaceView-->Canvas
【发布时间】:2013-01-04 23:18:37
【问题描述】:

我希望每次按下屏幕上的矩形按钮之一来改变位置。或刷新整个 SurfaceView。

应用程序似乎在运行,但矩形不随计数器改变位置。

非常感谢。

野兔是xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
    android:id="@+id/surface"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world"
    tools:context=".MainActivity" />
<Button
    android:id="@+id/l"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="16dp"
    android:text="L" />
<Button
    android:id="@+id/r"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="19dp"
    android:text="R" />
</RelativeLayout>

这里是 Java:

package com.example.rectangle;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
DrawView drawView;
Paint paint = new Paint();
int w, h, edge, axis_w;
int counter;
Button add, sub;
TextView display;

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    counter = 0;
    add = (Button) findViewById(R.id.r);
    sub = (Button) findViewById(R.id.l);
    display = (TextView) findViewById(R.id.textView1);

    add.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter += 10;
            display.setText("Total is" + counter);

        }
    });

    sub.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter -= 10;
            display.setText("Total is" + counter);
        }
    });

    SurfaceView surface = (SurfaceView) findViewById(R.id.surface);

    surface.getHolder().addCallback(new Callback() {

        public void surfaceCreated(SurfaceHolder holder) {

            Canvas canvas = holder.lockCanvas();

            w = canvas.getWidth();
            h = canvas.getHeight();
            edge = 5;
            axis_w = 3;
            // canvas.drawRect(left, top, right, bottom, paint)
            // Background
            paint.setColor(Color.rgb(255, 255, 255));
            canvas.drawRect(0, 0, w, h, paint);
            // Canvas Background Color
            paint.setColor(Color.rgb(0, 206, 209));
            // Borders
            canvas.drawRect(0, 0, w, edge, paint);
            canvas.drawRect(0, h - edge, w, h, paint);
            canvas.drawRect(0, 0, edge, h, paint);
            canvas.drawRect(w - edge, 0, w, h, paint);
            // Axis
            canvas.drawRect(((w - axis_w) / 2) + counter, 0,
                    ((w + axis_w) / 2) + counter, h, paint);
            canvas.drawRect(0, (h - axis_w) / 2, w, (h + axis_w) / 2, paint);

            holder.unlockCanvasAndPost(canvas);
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
        }

        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
        }
    });
}
}

【问题讨论】:

    标签: android canvas surfaceview


    【解决方案1】:

    好的。尝试另一种解决方案。尝试制作自己的 View 类,您将在其中绘制矩形。我为你准备了一些代码。根据您的需要进行定制。

    Simlpe XML

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mainL"
        >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, MainActivity"
        android:id="@+id/button"
        />
    </LinearLayout>
    

    和java代码

    public class MainActivity extends Activity
    {
        MyRectg myRectg;
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            myRectg = new MyRectg(this);
            ((LinearLayout)findViewById(R.id.mainL)).addView(myRectg);
            ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View arg0)
            {
                myRectg.invalidate();
            }
        });
    }
    
    public class MyRectg extends View
    {
        public MyRectg(Context c)
        {
            super(c);
        }
    
        @Override
        public void onDraw(Canvas c)
        {
            //this simulate new positions calculating
            Random rnd = new Random();
            Paint p = new Paint();
            p.setColor(Color.GREEN);
            c.drawRect(rnd.nextFloat()*100, rnd.nextFloat()*500, rnd.nextFloat()*300, rnd.nextFloat()*500, p);
        }
    }
    

    现在,当您单击按钮时画布会随机移动。这是你想要的吗?

    【讨论】:

    • 非常感谢您的回答,效果很好,我想做一些稍微不同的事情,但是重新计算到矩形坐标的主要思想是一样的。
    • 很遗憾,由于我的声誉很低,我无法投票赞成您的回答
    • 我刚刚按照自己想做的修改了代码,效果很好,再次感谢。
    【解决方案2】:

    尝试在 SurfaceView 上调用 Invalidate 方法以在每次位置更改后刷新它。

    编辑:

    并将位置计算从surfaceCreated移动到surfaceChanged方法。

    【讨论】:

    • 谢谢,但 Invalidate 方法仅适用于 Draw Canvas。我仍在处理您的第二个建议
    • 我也将画布放在了surfaceChanged方法中,但它似乎不起作用
    猜你喜欢
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多