/**
*本程序由070203110安樹峯軟件0705倡導,主編,賀鵬,賈飛,
*田鎮源, 王振興合作開發,仿冒必究
*/
package com.csdn.shufeng1988;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
/**
* @author Shufeng An
* @version 1.0.1
*
*/
public class GameView extends View implements Runnable
{
// 指示是否有鼠標點擊
boolean flag = false;
Bitmap mBitQQ = null;
int BitQQwidth = 0;
int BitQQheight = 0;
// 旋轉角度
float Angle = 0.0f;
// 放大倍數
float Scale = 1.0f;
//定義旋轉矩陣
Matrix mMatrix = new Matrix();
/* 定義Alpha動畫 */
private Animation mAnimationAlpha = null;
/* 定義Scale動畫 */
private Animation mAnimationScale = null;
/* 定義Translate動畫 */
private Animation mAnimationTranslate = null;
/* 定義Rotate動畫 */
private Animation mAnimationRotate = null;
//是否播放動畫
boolean flag2 = false;
public GameView(Context context)
{
super(context);
mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq))
.getBitmap();
BitQQwidth = mBitQQ.getWidth();
BitQQheight = mBitQQ.getHeight();
new Thread(this).start();
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run()
{
while (!Thread.currentThread().isInterrupted())
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
postInvalidate();
}
}
// 繪製圖像在屏幕上
public void onDraw(Canvas canvas)
{
// TODO Auto-generated method stub
super.onDraw(canvas);
mMatrix.reset();
mMatrix.setRotate(Angle);
mMatrix.postScale(Scale, Scale);
// 克隆一張圖片
Bitmap mBitQQ2 = Bitmap.createBitmap(mBitQQ, 0, 0, BitQQwidth,
BitQQheight, mMatrix, true);
if (flag)
{
GameView.drawImage(canvas, mBitQQ2, x, y);
flag2 = false;
}
else if (flag2)
{
canvas.drawBitmap(mBitQQ2, x, y, null);
//flag = false;
}
else
{
GameView.drawImage(canvas, mBitQQ2, (320 - BitQQwidth) / 2 - 20, 50);
}
mBitQQ2 = null;
}
// 按下2,3,4,5鍵播放對應的動畫
// 按下上下左右鍵對應圖片的縮小,放大,旋轉
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_DPAD_LEFT:
Angle--;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Angle++;
break;
case KeyEvent.KEYCODE_DPAD_UP:
if (Scale > 0.15)
{
Scale -= 0.1;
}
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
if (Scale < 3)
{
Scale += 0.1;
}
break;
case KeyEvent.KEYCODE_2:
flag2 = true;
/* 創建Alpha動畫 */
mAnimationAlpha = new AlphaAnimation(0.1f, 1.0f);
/* 設置動畫的時間 */
mAnimationAlpha.setDuration(3000);
/* 開始播放動畫 */
this.startAnimation(mAnimationAlpha);
break;
case KeyEvent.KEYCODE_3:
flag2 = true;
/* 創建Scale動畫 */
mAnimationScale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
/* 設置動畫的時間 */
mAnimationScale.setDuration(500);
/* 開始播放動畫 */
this.startAnimation(mAnimationScale);
break;
case KeyEvent.KEYCODE_4:
flag2 = true;
/* 創建Translate動畫 */
mAnimationTranslate = new TranslateAnimation(10, 100, 10, 100);
/* 設置動畫的時間 */
mAnimationTranslate.setDuration(1000);
/* 開始播放動畫 */
this.startAnimation(mAnimationTranslate);
break;
case KeyEvent.KEYCODE_5:
flag2 = true;
/* 創建Rotate動畫 */
mAnimationRotate = new RotateAnimation(0.0f, +360.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
/* 設置動畫的時間 */
mAnimationRotate.setDuration(1000);
/* 開始播放動畫 */
this.startAnimation(mAnimationRotate);
break;
}
return super.onKeyDown(keyCode, event);
}
// 當用戶點擊屏幕時,顯示圖片的座標
private float x = 0.0f;
private float y = 0.0f;
@Override
public boolean onTouchEvent(MotionEvent event)
{
x = event.getRawX();
// y座標減去50px,消除像素誤差
y = event.getRawY() - 50;
flag = true;
return super.onTrackballEvent(event);
}
//自定義函數用來畫轉換後的圖
public static void drawImage(Canvas canvas, Bitmap bitmap, float x, float y)
{
canvas.drawBitmap(bitmap, x, y, null);
}
}
//
package com.csdn.shufeng1988;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Activity01 extends Activity
{
// 檢查程序用
private static final String TAG = "Activity01";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//圖片瀏覽按鈕
Button button = (Button) findViewById(R.id.Button01);
//功能介紹按鈕
Button button2 = (Button) findViewById(R.id.Button02);
button.setTextColor(Color.RED);
button.setBackgroundColor(Color.GREEN);
button.setTextSize(20);
button2.setTextColor(Color.CYAN);
button.setTextSize(30);
//小組成員介紹按鈕
Button button3 = (Button) findViewById(R.id.Button03);
button3.setTextColor(Color.GREEN);
button3.setWidth(200);
button3.setTextSize(40);
button3.setBackgroundDrawable(getResources().getDrawable(
R.drawable.shufeng));
button.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setClass(Activity01.this, Activity02.class);
startActivity(intent);
Activity01.this.finish();
}
});
button2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast toast = Toast.makeText(Activity01.this,
"按上下左右鍵對應縮小放大順,逆時針旋轉" +
"按2,3,4,5,對應漸變、伸縮、轉換、旋轉動畫"
+"點擊鼠標可以移動圖片位置到指定位置",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 390);
toast.show();
Dialog dialog = new AlertDialog.Builder(Activity01.this)
.setTitle("提示").setMessage(
"按上、下、左、右鍵對應縮小、放大、順,逆時針旋轉"
+ "按2、3、4、5,對應漸變、伸縮、轉換、旋轉動畫"
+ "點擊鼠標可以移動圖片位置到指定位置")
.setPositiveButton("確定",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
dialog.cancel();
}
}).create();
dialog.show();
}
});
button3.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
Dialog dialog = new AlertDialog.Builder(Activity01.this)
.setTitle("提示").setMessage(
"070203110安樹峯,070203101田鎮源,070203090賈飛"
+ "070203089賀鵬,070203150王振興")
.setPositiveButton("確定",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which)
{
dialog.cancel();
}
}).create();
dialog.show();
}
});
}
public void onStart()
{
super.onStart();
Log.v(TAG, "onStart");
}
public void onResume()
{
super.onResume();
Log.v(TAG, "onResume");
}
public void onPause()
{
super.onPause();
Log.v(TAG, "onPause");
}
public void onStop()
{
super.onStop();
Log.v(TAG, "onStop");
}
public void onDestroy()
{
super.onDestroy();
Log.v(TAG, "onDestroy");
}
public void onRestart()
{
super.onRestart();
Log.v(TAG, "onReStart");
}
}
package com.csdn.shufeng1988;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
public class Activity02 extends Activity
{
private GameView mGameView = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mGameView = new GameView(this);
setContentView(mGameView);
}
public boolean onKeyUp(int keyCode, KeyEvent event)
{
super.onKeyUp(keyCode, event);
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ( mGameView == null )
{
return false;
}
if ( keyCode == KeyEvent.KEYCODE_BACK)
{
this.finish();
return true;
}
return mGameView.onKeyDown(keyCode,event);
}
}