Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android SurfaceView 運用實例詳解

Android SurfaceView 運用實例詳解

編輯:關於Android編程

package gongzi.co.cc;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;

public class SurfaceActivity extends
  Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(
   Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final SurfaceView surfaceView = (SurfaceView)

findViewById(R.id.surfaceView1);
  // surfaceView.setClickable(true);
  surfaceView
    .setOnClickListener(new

OnClickListener() {

     @Override
     public void onClick(
       View v) {
      // TODO Auto-generated

method stub
//      SurfaceView surfaceView

= (SurfaceView) v;
      Canvas canvas =

surfaceView
       

.getHolder()
       

.lockCanvas();
      Paint paint = new

Paint();

      paint.setColor

(Color.BLUE);
      paint.setAntiAlias

(true);
      paint.setTextSize(24);
      canvas.drawText(
       

"loveyou",
        0, 100,
        paint);
      surfaceView
       

.getHolder()
       

.unlockCanvasAndPost(
        

 canvas);

     }
    });
 }
}


---------------2:主main
package gongzi.co.cc;

import android.app.Activity;
import android.os.Bundle;

public class SurfaceActivity extends
  Activity {
 @Override
 protected void onCreate(
   Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(new Click(this));
 }

}

-----------SurfaceActivity.java
package gongzi.co.cc;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Click extends SurfaceView
  implements
  SurfaceHolder.Callback {

 public Click(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  getHolder().addCallback(this);
 }

 @Override
 public void surfaceChanged(
   SurfaceHolder holder,
   int format, int width,
   int height) {
  // TODO Auto-generated method stub

 }

 @Override
 public void surfaceCreated(
   SurfaceHolder holder) {
  // TODO Auto-generated method stub

  Canvas canvas = holder
    .lockCanvas();
  Paint paint = new Paint();
  paint.setColor(Color.BLUE);
  paint.setAntiAlias(true);
  paint.setTextSize(26);
  canvas.drawText(
    "love you gogogogogoigogoi",
    100, 100, paint);
  holder.unlockCanvasAndPost(canvas);

 }

 @Override
 public void surfaceDestroyed(
   SurfaceHolder holder) {
  // TODO Auto-generated method stub

 }

}

 

------------圖片和軌跡球
package gongzi.co.cc;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceView;

public class Click extends SurfaceView {
 float left;
 float top;
 Bitmap mBitmap = BitmapFactory
   .decodeResource(
     getContext()
       .getResources

(),
     R.drawable.ic_launcher);

 public Click(Context context) {
  super(context);

  // TODO Auto-generated constructor stub
 }

 @Override
 public boolean onTouchEvent(
   MotionEvent event) {
  // TODO Auto-generated method stub
  left = event.getX();
  top = event.getY();
  doDraw();
  return super
    .onTouchEvent(event);
 }

 @Override
 public boolean onTrackballEvent(
   MotionEvent event) {
  // TODO Auto-generated method stub
  left = event.getX();
  top = event.getY();
  doDraw();
  return super
    .onTrackballEvent(event);
 }

 private void doDraw() {
  // TODO Auto-generated method stub
  Canvas canvas = getHolder()
    .lockCanvas();
  canvas.drawBitmap(mBitmap,
    left, top, null);
  getHolder()
    .unlockCanvasAndPost(
      canvas);

 }

}

-------------------半透明 桌面
package gongzi.co.cc;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Click extends SurfaceView
  implements
  SurfaceHolder.Callback {
 float left;
 float top;
 Bitmap mBitmap = BitmapFactory
   .decodeResource(
     getContext()
       .getResources

(),
     R.drawable.ic_launcher);

 public Click(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  getHolder()
    .setFormat(
     

PixelFormat.TRANSLUCENT);
  getHolder().addCallback(this);
 }

 @Override
 public void surfaceChanged(
   SurfaceHolder holder,
   int format, int width,
   int height) {
  // TODO Auto-generated method stub

 }

 @Override
 public void surfaceCreated(
   SurfaceHolder holder) {
  // TODO Auto-generated method stub

  doDraw();

 }

 @Override
 public void surfaceDestroyed(
   SurfaceHolder holder) {
  // TODO Auto-generated method stub

 }

 @Override
 public boolean onTouchEvent(
   MotionEvent event) {
  // TODO Auto-generated method stub
  left = event.getX();
  top = event.getY();
  doDraw();
  return super
    .onTouchEvent(event);
 }

 void doDraw() {
  // TODO Auto-generated method stub
  Canvas canvas = getHolder()
    .lockCanvas();
  canvas.drawBitmap(mBitmap,
    left, top, null);
  getHolder()
    .unlockCanvasAndPost(
      canvas);

 }

}

---------manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="gongzi.co.cc"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SurfaceActivity"
            android:theme="@android:style/Theme.Translucent"
           
            >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category

android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

作者:gongzibai
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved