当前位置:操作系统 > 安卓/Android >>

Android Su易做图ceView 运用实例详解

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.Su易做图ceView;
import android.view.View;
import android.view.View.OnClickListener;

public class Su易做图ceActivity extends
  Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(
   Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  final Su易做图ceView su易做图ceView = (Su易做图ceView)

findViewById(R.id.su易做图ceView1);
  // su易做图ceView.setClickable(true);
  su易做图ceView
    .setOnClickListener(new

OnClickListener() {

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

method stub
//      Su易做图ceView su易做图ceView

= (Su易做图ceView) v;
      Canvas canvas =

su易做图ceView
       

.getHolder()
       

.lockCanvas();
      Paint paint = new

Paint();

      paint.setColor

(Color.BLUE);
      paint.setAntiAlias

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

"loveyou",
        0, 100,
        paint);
      su易做图ceView
       

.getHolder()
       

.unlockCanvasAndPost(
        

 canvas);

     }
    });
 }
}


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

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

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

}

-----------Su易做图ceActivity.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.Su易做图ceHolder;
import android.view.Su易做图ceView;

public class Click extends Su易做图ceView
  implements
  Su易做图ceHolder.Callback {

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

 @Override
 public void su易做图ceChanged(
   Su易做图ceHolder holder,
   int format, int width,
   int height) {
  // TODO Auto-generated method stub

 }

 @Override
 public void su易做图ceCreated(
   Su易做图ceHolder 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 su易做图ceDestroyed(
   Su易做图ceHolder 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.Su易做图ceView;

public class Click extends Su易做图ceView {
 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.Su易做图ceHolder;
import android.view.Su易做图ceView;

public class Click extends Su易做图ceView
  implements
  Su易做图ceHolder.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 su易做图ceChanged(
   Su易做图ceHolder holder,
   int format, int width,
   int height) {
  // TODO Auto-generated method stub

 }

 @Override
 public void su易做图ceCreated(
   Su易做图ceHolder holder) {
  // TODO Auto-generated method stub

  doDraw();

 }

 @Override
 public void su易做图ceDestroyed(
 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,