刷新SurfaceView - >画布上OnClickListener的Andr​​oid [英] Refresh SurfaceView-->Canvas on OnClickListener Android

查看:246
本文介绍了刷新SurfaceView - >画布上OnClickListener的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次想我preSS的按钮在屏幕上的矩形来改变位置中的一个。或整个SurfaceView刷新。

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

非常感谢你。

野兔是XML code:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent>
< SurfaceView
    机器人:ID =@ + ID /面
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent/>
<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:文字=@字符串/参考hello world
    工具:上下文=。MainActivity/>
<按钮
    机器人:ID =@ + ID /升
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:layout_marginLeft =16DP
    机器人:文字=L/>
<按钮
    机器人:ID =@ + ID / R
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignLeft =@ + ID / textView1
    机器人:layout_alignParentTop =真
    机器人:layout_marginLeft =19dp
    机器人:文字=R/>
< / RelativeLayout的>

下面是Java:

 包com.example.rectangle;进口android.app.Activity;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.os.Bundle;
进口android.view.SurfaceHolder;
进口android.view.SurfaceHolder.Callback;
进口android.view.SurfaceView;
进口android.view.View;
进口android.view.Window;
进口android.view.WindowManager;
进口android.widget.Button;
进口android.widget.TextView;公共类MainActivity延伸活动{
DrawView drawView;
涂料粉刷=新的油漆();
INT W,H,边缘,axis_w;
INT计数器;
按钮添加,子;
TextView中显示;@覆盖
公共无效的onCreate(捆绑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);    计数器= 0;
    添加=(按钮)findViewById(R.id.r);
    分=(按钮)findViewById(R.id.l);
    显示=(的TextView)findViewById(R.id.textView1);    add.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            计数器+ = 10;
            display.setText(全是+计数器);        }
    });    sub.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            计数器 - = 10;
            display.setText(全是+计数器);
        }
    });    SurfaceView表面=(SurfaceView)findViewById(R.id.surface);    surface.getHolder()的addCallback(新回拨(){        公共无效surfaceCreated(SurfaceHolder持有人){            帆布帆布= holder.lockCanvas();            W = canvas.getWidth();
            H = canvas.getHeight();
            边= 5;
            axis_w = 3;
            // canvas.drawRect(左,上,右,下,涂料)
            //背景
            paint.setColor(Color.rgb(255,255,255));
            canvas.drawRect(0,0,W,H,油漆);
            //画布背景颜色
            paint.setColor(Color.rgb(0,206,209));
            // 国界
            canvas.drawRect(0,0,W,边缘,油漆);
            canvas.drawRect(0,H - 边,W,H,油漆);
            canvas.drawRect(0,0,边缘,小时,漆);
            canvas.drawRect(重量 - 边缘,0,W,H,油漆);
            //轴
            canvas.drawRect(((W - axis_w)/ 2)+反,0,
                    ((W + axis_w)/ 2)+反,小时,漆);
            canvas.drawRect(0,(H - axis_w)/ 2,W,(H + axis_w)/ 2,油漆);            holder.unlockCanvasAndPost(画布);
        }        公共无效surfaceDestroyed(SurfaceHolder持有人){
        }        公共无效surfaceChanged(SurfaceHolder架,INT格式,
                INT宽度,高度INT){
        }
    });
}
}


解决方案

确定。尝试另一种解决方案。尽量让自己的视图类,您将绘制矩形。我有prepared一些code你。它自定义您的需求。

Simlpe XML
    

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:ID =@ + ID /成为内地
    >
<按钮
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=世界您好,MainActivity
    机器人:ID =@ + ID /按钮
    />
< / LinearLayout中>

和java code

 公共类MainActivity扩展活动
{
    MyRectg myRectg;
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        myRectg =新MyRectg(本);
        ((的LinearLayout)findViewById(R.id.mainL))addView(myRectg)。
        ((按钮)findViewById(R.id.button))setOnClickListener。(新View.OnClickListener(){        公共无效的onClick(查看arg0中)
        {
            myRectg.invalidate();
        }
    });
}公共类MyRectg扩展视图
{
    公共MyRectg(上下文C)
    {
        超(C);
    }    @覆盖
    公共无效的onDraw(帆布C)
    {
        //这个模拟的新职位计算
        随机RND =新的随机();
        涂料P =新的油漆();
        p.setColor(Color.GREEN);
        c.drawRect(rnd.nextFloat()* 100,rnd.nextFloat()* 500,rnd.nextFloat()* 300,rnd.nextFloat()* 500,p)的;
    }
}

现在,当您单击该按钮帆布随意移动。是你想请问这是什么?

I would like each time I press one of the buttons the rectangles on the screen to change position. Or the entire SurfaceView to refresh.

The app seems to run but the rectangles do not change position with the counter.

Thank you very much.

Hare is the xml code:

<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>

Here is the 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) {
        }
    });
}
}

解决方案

Ok. Try Another solution. Try to make your own View class where you will draw your rectangle. I have prepared some code for you. Customize it to your needs.

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>

and java code

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);
    }
}

Now canvas randomly moves when you click the button. Is this what you wanted?

这篇关于刷新SurfaceView - &GT;画布上OnClickListener的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆