暇人プログラマの日記
フリーランサーに憧れる暇人プログラマの日記
Handler handler = new Handler();
Runnable runnable;
//…省略
new Thread() {
public void run() {
handler.post(runnable);
}
}.start();
runnable = new Runnable(){
public void run(){
//適当な処理
}
};
import android.app.Activity;
import android.os.Bundle;
public class Trump extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TrumpView(this));
}
}
import java.util.Random;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class TrumpView extends SurfaceView
implements SurfaceHolder.Callback,Runnable {
SurfaceHolder holder;
Thread thread;
Card[] card = new Card[52];
int preClick = -1;
public TrumpView(Context context) {
super(context);
Resources r = context.getResources();
int[] num = new int[52];
for(int i = 0;i < 52;i++){
num[i] = i % 13 + 1;
}
num = shuffle(num);
int x = 0;
int y = 0;
//カード格納ループ
for(int i = 0;i < 52;i++){
switch(num[i]){
case 1:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t1),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 2:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t2),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 3:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t3),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 4:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t4),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 5:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t5),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 6:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t6),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 7:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t7),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 8:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t8),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 9:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t9),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 10:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t10),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 11:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t11),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 12:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t12),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
case 13:
card[i] = new Card(
BitmapFactory.decodeResource(r, R.drawable.t13),
BitmapFactory.decodeResource(r, R.drawable.reverse),num[i],x,y);
break;
default:
break;
}
x = card[i].getEndX();
if((i + 1) % 8 == 0){
x = 0;
y = card[i].getEndY();
}
}
holder = getHolder();
holder.addCallback(this);
holder.setFixedSize(getWidth(), getHeight());
}
@Override
public boolean onTouchEvent(MotionEvent motion){
if(motion.getAction() != MotionEvent.ACTION_DOWN){return true;}
int x = (int)motion.getX();
int y = (int)motion.getY();
for(int i = 0;i < card.length;i++){
//タッチされた場所に画像が存在していた場合
if(card[i].getStartX() <= x && card[i].getEndX() >= x &&
card[i].getStartY() <= y && card[i].getEndY() >= y){
//カードが開いていなかった場合
if(!card[i].isClick()){
card[i].onClick();
//カードを2枚開いていない場合
if(preClick == -1){
preClick = i;
}else if(preClick != i){
try{
Thread.sleep(1000);
}catch(Exception e){}
//開いた2枚のカードの値が一致しなかった場合
if(card[i].getNumber() != card[preClick].getNumber()){
card[i].onClick();
card[preClick].onClick();
}
preClick = -1;
}
}
break;
}
}
return true;
}
public int[] shuffle(int[] num){
Random rand = new Random();
int rnum;
int temp;
for(int i = 52 - 1;i > 0;i--){
rnum = rand.nextInt(i);
temp = num[i];
num[i] = num[rnum];
num[rnum] = temp;
}
return num;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}
@Override
public void surfaceCreated(SurfaceHolder holder) {
thread = new Thread(this);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
thread = null;
}
@Override
public void run() {
Canvas canvas;
while(thread != null){
canvas = holder.lockCanvas();
for(Card c : card){
canvas.drawBitmap(c.getImage(),c.getStartX(),c.getStartY(),null);
}
holder.unlockCanvasAndPost(canvas);
}
}
}
import android.graphics.Bitmap;
public class Card {
private final Bitmap frontCard;
private final Bitmap reverseCard;
private final int number;
private int x;
private int y;
private boolean flag;
public Card(Bitmap frontCard,Bitmap reverseCard,int number){
this.frontCard = frontCard;
this.reverseCard = reverseCard;
this.number = number;
}
public Card(Bitmap frontCard,Bitmap reverseCard,int number,int x,int y){
this.frontCard = frontCard;
this.reverseCard = reverseCard;
this.number = number;
this.x = x;
this.y = y;
}
public void setStartX(int x){
this.x = x;
}
public void setStartY(int y){
this.y = y;
}
public int getStartX(){
return x;
}
public int getStartY(){
return y;
}
public int getEndX(){
return x + frontCard.getWidth();
}
public int getEndY(){
return y + frontCard.getHeight();
}
public Bitmap getImage(){
if(flag) return frontCard;
else return reverseCard;
}
public int getNumber(){
return number;
}
public void onClick(){
flag = !flag;
}
public boolean isClick(){
return flag;
}
}
SurfaceHolder holder;
holder = getHolder();
holder.addCallback(this);
holder.setFixedSize(getWidth(), getHeight());
import android.app.Activity;
import android.os.Bundle;
public class ImageTouchTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ImageTouchView(this));
}
}
import android.content.Context;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
public class ImageTouchView extends View{
private FieldImage[] image = new FieldImage[4];
public ImageTouchView(Context context){
super(context);
Resources r = context.getResources();
image[0] = new FieldImage(BitmapFactory.decodeResource(r, R.drawable.map),0,0);
image[1] = new FieldImage(BitmapFactory.decodeResource(r, R.drawable.map),40,0);
image[2] = new FieldImage(BitmapFactory.decodeResource(r, R.drawable.map),80,0);
image[3] = new FieldImage(BitmapFactory.decodeResource(r, R.drawable.map),0,40);
}
@SuppressWarnings("static-access")
@Override
public boolean onTouchEvent(MotionEvent motion){
if(motion.getAction() != motion.ACTION_DOWN){return true;}
int x = (int)motion.getX();
int y = (int)motion.getY();
for(int i = 0;i < image.length;i++){
//タッチされた場所に画像が存在していた場合
if(image[i].getStartX() <= x && image[i].getEndX() >= x &&
image[i].getStartY() <= y && image[i].getEndY() >= y){
try{
image[i].changeAllColor(Color.RED);
//再描画
invalidate();
break;
}catch(Exception e){}
}
}
return true;
}
//テスト用の画像を描画する
protected void onDraw(Canvas canvas){
for(FieldImage i : image){
canvas.drawBitmap(i.image,i.getStartX(),i.getStartY(),null);
}
}
}
import android.graphics.Bitmap;
public class FieldImage {
Bitmap image;
private int x;
private int y;
public FieldImage(Bitmap image){
this.image = image;
}
public FieldImage(Bitmap image,int x,int y){
this.image = image;
this.x = x;
this.y = y;
}
public void setStartX(int x){
this.x = x;
}
public void setStartY(int y){
this.y = y;
}
public int getStartX(){
return x;
}
public int getStartY(){
return y;
}
public int getEndX(){
return x + image.getWidth();
}
public int getEndY(){
return y + image.getHeight();
}
public void changeAllColor(int color){
int[] pixel = new int[image.getWidth() * image.getHeight()];
for(int i = 0;i < image.getWidth() * image.getHeight();i++){
pixel[i] = color;
}
Bitmap imageCopy = image.copy(Bitmap.Config.ARGB_8888, true);
imageCopy.setPixels(pixel, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
image = imageCopy;
}
}
import android.app.Activity;
import android.os.Bundle;
public class TouchTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TouchView(this));
}
}
import android.app.AlertDialog;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
public class TouchView extends View{
public TouchView(Context context){
super(context);
}
@SuppressWarnings("static-access")
@Override
public boolean onTouchEvent(MotionEvent motion){
if(motion.getAction() == motion.ACTION_DOWN){
AlertDialog.Builder alert = new AlertDialog.Builder(this.getContext());
alert.setMessage("X = " + motion.getX() + " Y = " + motion.getY());
alert.setPositiveButton("OK", null);
alert.create();
alert.show();
}
return true;
}
}
import android.app.Activity;
import android.os.Bundle;
public class MusicTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MusicView(this));
}
}
import android.content.Context;
import android.media.MediaPlayer;
import android.view.View;
public class MusicView extends View{
private MediaPlayer player;
public MusicView(Context context){
super(context);
try{
player = MediaPlayer.create(context, R.raw.tamn09);
//player.prepare();
player.setLooping(true);
player.seekTo(0);
player.start();
}catch(Exception e){}
}
}
Author:orz=3
最近フリーランサーに憧れている
暇人の日記です。
実務ではJava、Oracleをメインで
使用しているへっぽこプログラマです
用のある方は下記アドレスに連絡下さい
hogehoge0604☆gmail.com
2011/10/01から長期間お休みに入るので
お仕事の相談もお待ちしております
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
- | - | - | - | - | - | 1 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 | - | - | - | - | - |