如何在屏幕上绘制Android Studio?

时间:2015-03-14 01:37:14

标签: android screen draw

我是开发应用程序的初学者,有人可以发送简单的代码,或解释我如何在屏幕上绘制内容?

1 个答案:

答案 0 :(得分:1)

这是一个可以帮助你的代码。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;


public class rodando extends ActionBarActivity implements View.OnTouchListener{

    OurView v;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rodando);
        getSupportActionBar().hide();
        v = new OurView(this);
        v.setOnTouchListener(this);
        setContentView(v);

    }

    protected void onResume()
    {
        super.onResume();
        v.resume();
    }

    protected void onPause()
    {
        super.onPause();
        v.pause();
    }


    public class OurView extends SurfaceView implements Runnable
    {


        Thread t = null;
        SurfaceHolder holder;
        boolean isItOK = false;



        public OurView(Context context) {
            super(context);
            holder = getHolder();
        }

        @Override
        public void run() {


            while(isItOK == true)
            {
                Canvas c = holder.lockCanvas();

                if(!holder.getSurface().isValid())
                {
                    continue;
                }
                holder.unlockCanvasAndPost(c);

            }
        }

        public void pause()
        {
            isItOK = false;
            while(true)
            {
                try{
                    t.join();
                }catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
                break;
            }
            t = null;
        }

        public void resume()
        {
            isItOK = true;
            t = new Thread(this);

            t.start();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_rodando, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        
return true;
        }
        
}

您可以使用画布变量

进行绘制