2014年10月14日 星期二

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

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


 


此範例為程式碼(05\Sample08)的改寫,利用一個LinearLayout、一個SampleView、一個ViewFlipper來實作GUI界面,建立自己的View元件,並透過ViewFlipper 視圖的切換容器視圖,且實作偵測使用者左右滑動畫面事件,來驅動切換VIEW的內容使用範例,程式碼如下所示:


 


package com.jashsample;

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

import android.os.Bundle;
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.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.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元件,透過ViewFlipper 視圖的切換容器視圖,並實作偵測使用者左右滑動畫面事件,並驅動切換VIEW的內容。
 */
public class MainActivity extends Activity {
static final int num = 100;
ViewFlipper vf;//ViewFlipper 視圖的切換容器視圖
SampleView[] sv = new SampleView[3];//建立三個VIEW
LinearLayout[] ll = new LinearLayout[3];
float x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout llp = new LinearLayout(this);
setContentView(llp);

vf = new ViewFlipper(this);

for(int i=0; i<sv.length;i++)
{
ll[i] = new LinearLayout(this);
sv[i] = new SampleView(this);
ll[i].addView(sv[i]);
vf.addView(ll[i]);
}

llp.addView(vf);
setContentView(llp);

llp.setOnTouchListener(new SampleTouchListener());
}
class SampleTouchListener implements OnTouchListener//實作偵測使用者左右滑動畫面事件,並驅動切換VIEW的內容
{
public boolean onTouch(View v, MotionEvent e)
{
if(e.getAction() == MotionEvent.ACTION_DOWN)
{
x = e.getX();
}
else if(e.getAction() == MotionEvent.ACTION_UP)
{
if(x-20 > e.getX())
{
TranslateAnimation inanim = new TranslateAnimation(sv[0].getWidth(), 0, 0, 0);
inanim.setDuration(1000);
TranslateAnimation outanim = new TranslateAnimation(0,-sv[0].getWidth(), 0, 0);
outanim.setDuration(1000);
vf.setInAnimation(inanim);
vf.setOutAnimation(outanim);
vf.showNext();
}
else if(x+20 < e.getX())
{
TranslateAnimation inanim = new TranslateAnimation(-sv[0].getWidth(), 0, 0, 0);
inanim.setDuration(1000);
TranslateAnimation outanim = new TranslateAnimation(0,sv[0].getWidth(), 0, 0);
outanim.setDuration(1000);
vf.setInAnimation(inanim);
vf.setOutAnimation(outanim);
vf.showPrevious();
}
}
return true;
}
}
class SampleView extends View
{
ArrayList<Ball> bl;
float x, y;
public SampleView(Context cn)
{
super(cn);
bl = new ArrayList<Ball>();
Random rn = new Random();
for (int i=0; i<num; i++)
{
Ball b = new Ball();

b.x = (int)x+rn.nextInt(500);
b.y = (int)y+rn.nextInt(1000);
b.r = rn.nextInt(256);
b.g = rn.nextInt(256);
b.b = rn.nextInt(256);
bl.add(b);
}
}
/*
//由於要在LinearLayout實作偵測使用者左右滑動畫面事件,因此必須將VIEW的onTouchEvent關閉,否則LinearLayout事件會無效
public boolean onTouchEvent(MotionEvent e)
{
x = e.getX();
y = e.getY();
this.invalidate();//觸發onDraw
Random rn = new Random();
bl.clear();
for (int i=0; i<num; i++)
{
Ball b = new Ball();

b.x = (int)x+rn.nextInt(500);
b.y = (int)y+rn.nextInt(1000);
b.r = rn.nextInt(256);
b.g = rn.nextInt(256);
b.b = rn.nextInt(256);
bl.add(b);
}
return true;
}
*/
protected void onDraw(Canvas cs)
{
super.onDraw(cs);
Random rn = new Random();
Paint p = new Paint();
for(int i=0; i<num; i++)
{
Ball b = bl.get(i);

p.setColor(Color.rgb(b.r,b.g,b.b));
p.setStyle(Paint.Style.FILL);
cs.drawCircle(b.x,b.y,10,p);
}
}
}
}
class Ball
{
public int x,y,r,g,b;
}
















 






沒有留言:

張貼留言