2014年10月15日 星期三

android程式設計實例入門 -Sample改寫分享 (2014/10/16)

android程式設計實例入門 -Sample改寫分享 (2014/10/16)


 


此範例為程式碼(05\Sample09)的改寫,利用一個LinearLayout、一個SampleView實作GUI界面,建立自己的View元件,並透過Activity implements Runnable配合Handler來實作定時(Timer)繪圖實現彈跳球效果使用範例,程式碼如下所示:


 


package com.jashsample;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Random;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.format.DateFormat;
import android.text.method.KeyListener;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ViewFlipper;
/*
 * 建立自己的View元件,外加透過Activity implements Runnable配合Handler來實作定時(Timer)繪圖實現彈跳球效果。
 */
public class MainActivity extends Activity implements Runnable{
SampleView sv;
Handler hn;
float x, y, dx, dy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);

LinearLayout ll = new LinearLayout(this);
setContentView(ll);

hn = new Handler();
hn.postDelayed(this, 10);

sv = new SampleView(this);

ll.addView(sv);
}
public void run()
{
WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
Display dp = wm.getDefaultDisplay();

if(x<0 || x>dp.getWidth()) dx = -dx;
if(y<0 || y>dp.getHeight()) dy = -dy;

x+=dx;
y+=dy;

sv.invalidate();

hn.postDelayed(this, 10);
}
public void onDestroy()
{
super.onDestroy();
hn.removeCallbacks(this);
}

class SampleView extends View
{
public SampleView(Context cn)
{
super(cn);
x = 0; y = 0;
dx = 10; dy = 10;
}
protected void onDraw(Canvas cs)
{
super.onDraw(cs);
Paint p = new Paint();
p.setColor(Color.BLUE);
p.setStyle(Paint.Style.FILL);
cs.drawCircle(x, y, 10, p);
}
}
}
class Ball
{
public int x,y,r,g,b;
}
















 






沒有留言:

張貼留言