2011年10月31日 星期一

Some Notes about Android APPs programming

Random number
import java.util.Random;
private static final Random rand = new Random();
rand.nextInt(xxx);
nextInt(int n) - Returns a pseudo-random uniformly distributed int in the half-open range [0, n).

Math Lab
import java.lang.Math;
Math.pow();

Create new class
In Eclipse, choose File → New → Class.
Type Class Name
Superclass
android.appActivity
android.database.sqlite.SQLiteOpenHelper
and check the box for “Constructors from superclass.”

android:textcolor
A color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
Example:
android:textcolor="#000"  or "#000000" => BLACK
android:textcolor="#FFF" or "#FFFFFF"  => WHITE
android:textcolor="#F00" or "#FF0000"  => RED
android:textcolor="#0F0" or #00FF00"  => GREEN
android:textcolor="#00F" or "#0000FF"  => BLUE


Android SurfaceView
1. Add new SurfaceView Class

2. Add new Thread Class name

3. Add code in the Thread
Override run()
4. Add code in the SurfaceView
Override surfaceCreated & surfaceDestroyed

Android Rotate Text
@Override
 public void onDraw(Canvas c) {
  super.onDraw(c);
// ........,.
  // Draw Rotated text
  paint.setColor(Color.RED); 
  paint.setTextSize(30); 
  String rotate_str = "Rotate TEXT";
  // rotate the canvas on center of the text to draw 
  c.rotate(45, 50, 50);  // rotate 45 at x = 50 and y = 50
  // draw the rotated text 
  paint.setStyle(Paint.Style.FILL); 
  c.drawText(rotate_str, 50, 50, paint); 
  //undo the rotate 
  c.restore();
// ..........

Android Draw using View Class
MyView.java
package com.hong.testing;
import android.app.Activity;
import android.os.Bundle;
public class MyView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new Panel(this));    }
}
panel.java
package com.hong.testing;
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.Rect;
import android.view.View;
class Panel extends View {

 public Panel(Context context) {
  super(context);
 }

 @Override public void onDraw(Canvas c) {
  super.onDraw(c);
  Paint paint = new Paint(); 
  paint.setStyle(Paint.Style.FILL);
  // make the entire canvas white 
  paint.setColor(Color.WHITE); 
  c.drawPaint(paint);

     // Draw Line     paint.setColor(Color.LTGRAY);
     paint.setStrokeWidth(3);
     paint.setColor(Color.LTGRAY);
     c.drawLine(0, 0, c.getWidth()-1, c.getHeight()-1, paint);
     c.drawLine(c.getWidth()-1, 0,0 , c.getHeight()-1, paint);
  // Put Bitmap  Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
     c.drawBitmap(bitmap, 10, 10, null);
   
  // Draw a blue circle  paint.setAntiAlias(true); 
  paint.setColor(Color.BLUE); 
  c.drawCircle(20, 20, 10, paint);  // x = 20, y = 20 radius = 10
  // Draw a green rectangle  paint.setColor(Color.GREEN); 
  c.drawRect(35, 10, 60, 30, paint); // x1 = 35, y1 = 10, x2 = 60, y2 = 30

  // Draw red text   paint.setColor(Color.RED); 
  paint.setStyle(Paint.Style.FILL); 
  paint.setAntiAlias(true); 
  paint.setTextSize(30); 
  c.drawText("Text Drawing", 30, 100, paint);
  }   
}

沒有留言:

張貼留言