2013年12月30日 星期一

利用FrameLayout+ImageView+鍵盤 實現駐點移動效果

利用FrameLayout+ImageView+鍵盤 實現駐點移動效果



 


因為要實作player 控制面板,所以線撰寫測試範例,歡迎有興同好,一起來C/P一下。


PS.由於該程式需要利用到圖片檔,但是我又懶得利用部落格上傳,所以直接使用GOOGLE空間


網址如下:https://drive.google.com/file/d/0B_1Q3Cz3GV5RdnNvdDJ4TzJNQnczWUZNM2lsdkp0Y2tJZkEw/edit?usp=sharing


 


 









package com.example.imageview_click_keyevent;

import com.example.imageview_click_keyevent.MainActivity.ButListener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
///////////////////////////////////////

import android.view.View;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {
ImageView[] IV; //Button[] bt;
DisplayMetrics metrics;
int[] m_changeindex;
Bitmap bmp;
int intfouce;
int intlastenter;
final int IV_base=198105020;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
metrics = new DisplayMetrics();//抓取螢幕大小
getWindowManager().getDefaultDisplay().getMetrics(metrics);
/////////////////////////////////////////
FrameLayout Fralayout = new FrameLayout(this);
FrameLayout.LayoutParams lparam = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels);
Fralayout.setLayoutParams(lparam);
this.setContentView(Fralayout);
////////////////////////////////////////
IV=new ImageView[4];
IV[0]=new ImageView(this);
IV[0].setId(IV_base);
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.left02);
IV[0].setImageBitmap(bmp);
//IV[0].getLayoutParams().width= (metrics.widthPixels/2);
//IV[0].getLayoutParams().height = (metrics.heightPixels/2);
IV[0].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams0.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams0.leftMargin = 0;
layoutParams0.topMargin = 0;
IV[0].setLayoutParams(layoutParams0);
Fralayout.addView(IV[0]);

IV[1]=new ImageView(this);
IV[1].setId((IV_base+1));
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.right02);
IV[1].setImageBitmap(bmp);
IV[1].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams1.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams1.leftMargin = metrics.widthPixels/2;
layoutParams1.topMargin = 0;
IV[1].setLayoutParams(layoutParams1);
Fralayout.addView(IV[1]);

IV[2]=new ImageView(this);
IV[2].setId((IV_base+2));
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.start03);
IV[2].setImageBitmap(bmp);
IV[2].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams2.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams2.leftMargin = 0;
layoutParams2.topMargin = metrics.heightPixels/2;
IV[2].setLayoutParams(layoutParams2);
Fralayout.addView(IV[2]);

IV[3]=new ImageView(this);
IV[3].setId((IV_base+3));
bmp=BitmapFactory.decodeResource(getResources(), R.drawable.stop02);
IV[3].setImageBitmap(bmp);
IV[3].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams3 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams3.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams3.leftMargin = metrics.widthPixels/2;
layoutParams3.topMargin = metrics.heightPixels/2;
IV[3].setLayoutParams(layoutParams3);
Fralayout.addView(IV[3]);

///*
m_changeindex=new int[4];
for(int i=0;i<4;i++)
{
m_changeindex[i]=0;
}
//*/

intfouce=2;//預設第一個按鈕有 駐點
intlastenter=2;
}
public boolean onKeyDown(int keycode, KeyEvent e){
String str;
str="";
int check_enter;
/////////////////////////////////////////
//把所有按鈕都設定為 沒駐點
IV[0].setImageResource(R.drawable.left02);
IV[1].setImageResource(R.drawable.right02);
IV[2].setImageResource(R.drawable.start02);
IV[3].setImageResource(R.drawable.stop02);
check_enter=0;

switch(keycode)
{
case KeyEvent.KEYCODE_DPAD_UP:
switch(intfouce)
{
case 0:
intfouce=2;
break;
case 1:
intfouce=3;
break;
case 2:
intfouce=0;
break;
case 3:
intfouce=1;
break;
}
str = "上";
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
switch(intfouce)
{
case 0:
intfouce=2;
break;
case 1:
intfouce=3;
break;
case 2:
intfouce=0;
break;
case 3:
intfouce=1;
break;
}
str = "下";
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
switch(intfouce)
{
case 0:
intfouce=1;
break;
case 1:
intfouce=0;
break;
case 2:
intfouce=3;
break;
case 3:
intfouce=2;
break;
}
str = "左";
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
switch(intfouce)
{
case 0:
intfouce=1;
break;
case 1:
intfouce=0;
break;
case 2:
intfouce=3;
break;
case 3:
intfouce=2;
break;
}
str = "右";
break;
case KeyEvent.KEYCODE_ENTER:
//依照駐點所在位置改變圖形
check_enter=1;
switch(intfouce)
{
case 0:
IV[0].setImageResource(R.drawable.left03);
break;
case 1:
IV[1].setImageResource(R.drawable.right03);
break;
case 2:
IV[2].setImageResource(R.drawable.start03);
break;
case 3:
IV[3].setImageResource(R.drawable.stop03);
break;
}
intlastenter=intfouce;
str = "ENTER";
break;
default:
str = "exit";
this.finish();
}

/////////////////////////////////////////
//依照駐點所在位置改變圖形
if(check_enter==0)
{
//////////////////////////////////////////////
switch(intfouce)
{
case 0:
IV[0].setImageResource(R.drawable.left04);
break;
case 1:
IV[1].setImageResource(R.drawable.right04);
break;
case 2:
IV[2].setImageResource(R.drawable.start04);
break;
case 3:
IV[3].setImageResource(R.drawable.stop04);
break;
}

switch(intlastenter)
{
case 0:
if(intfouce==intlastenter)
{
IV[0].setImageResource(R.drawable.left03);
}
else
{
IV[0].setImageResource(R.drawable.left01);
}
break;
case 1:
if(intfouce==intlastenter)
{
IV[1].setImageResource(R.drawable.right03);
}
else
{
IV[1].setImageResource(R.drawable.right01);
}
break;
case 2:
if(intfouce==intlastenter)
{
IV[2].setImageResource(R.drawable.start03);
}
else
{
IV[2].setImageResource(R.drawable.start01);
}
break;
case 3:
if(intfouce==intlastenter)
{
IV[3].setImageResource(R.drawable.stop03);
}
else
{
IV[3].setImageResource(R.drawable.stop01);
}
break;
}
}
//Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

return super.onKeyDown(keycode,e);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ButListener implements OnClickListener
{
public void onClick(View v)
{
switch(v.getId())
{
case IV_base:
m_changeindex[0]++;
if(m_changeindex[0]>3)
m_changeindex[0]=0;
switch(m_changeindex[0])
{
case 0:
IV[0].setImageResource(R.drawable.left01);
break;
case 1:
IV[0].setImageResource(R.drawable.left02);
break;
case 2:
IV[0].setImageResource(R.drawable.left03);
break;
case 3:
IV[0].setImageResource(R.drawable.left04);
break;
}
break;
case (IV_base+1):
m_changeindex[1]++;
if(m_changeindex[1]>3)
m_changeindex[1]=0;
switch(m_changeindex[1])
{
case 0:
IV[1].setImageResource(R.drawable.right01);
break;
case 1:
IV[1].setImageResource(R.drawable.right02);
break;
case 2:
IV[1].setImageResource(R.drawable.right03);
break;
case 3:
IV[1].setImageResource(R.drawable.right04);
break;
}
break;
case (IV_base+2):
m_changeindex[2]++;
if(m_changeindex[2]>3)
m_changeindex[2]=0;
switch(m_changeindex[2])
{
case 0:
IV[2].setImageResource(R.drawable.start01);
break;
case 1:
IV[2].setImageResource(R.drawable.start02);
break;
case 2:
IV[2].setImageResource(R.drawable.start03);
break;
case 3:
IV[2].setImageResource(R.drawable.start04);
break;
}
break;
case (IV_base+3):
m_changeindex[3]++;
if(m_changeindex[3]>3)
m_changeindex[3]=0;
switch(m_changeindex[3])
{
case 0:
IV[3].setImageResource(R.drawable.stop01);
break;
case 1:
IV[3].setImageResource(R.drawable.stop02);
break;
case 2:
IV[3].setImageResource(R.drawable.stop03);
break;
case 3:
IV[3].setImageResource(R.drawable.stop04);
break;
}
break;
}
}

}
}


 


沒有留言:

張貼留言