編輯:關於Android編程
1.
2.<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">
//使用回調接口,首先初始化pintuview並綁定,實現回調接口的方法
mPintuLayout = (PintuLayout) findViewById(R.id.mpintu);
mPintuLayout.setOnGamePintuListener(new GamePintuListener() {
@Override
public void timechanged(int currentTime) {
tvTime.setText(currentTime + "");
}
@Override
public void nextLevel(final int nextLevel) {
mtvNextLevel.setVisibility(0);
mPintuLayout.pause();
mPintuLayout.nextLevel();
level = nextLevel + "";
}
@Override
public void gameover() {
mtvGameOver.setVisibility(0);
}
});
}
@Override
protected void onPause() {
super.onPause();
mPintuLayout.pause();
}
@Override
protected void onResume() {
super.onResume();
mPintuLayout.resume();
}
// 設置按兩次回退退出程序
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
Toast.makeText(getApplicationContext(), "再按一次則退出",
Toast.LENGTH_SHORT).show();
return false;
}
return super.onKeyDown(keyCode, event);
}
private void exit() {
if (!isExit) {
isExit = true;
myHandler.sendEmptyMessageDelayed(0, 2000);
} else {
finish();
}
}
Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
isExit = false;
}
};
//界面的點擊事件
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.textView1:
mPintuLayout.pause();
Toast.makeText(getApplicationContext(), "Pausing",
Toast.LENGTH_SHORT).show();
mPintuLayout.setVisibility(4);
miv.setVisibility(0);
break;
case R.id.textView2:
mPintuLayout.resume();
Toast.makeText(getApplicationContext(), "Restarted",
Toast.LENGTH_SHORT).show();
mPintuLayout.setVisibility(0);
miv.setVisibility(4);
break;
case R.id.mtvGameOver:
mtvGameOver.setVisibility(4);
mPintuLayout.restart();
break;
case R.id.mtvNextLevel:
mtvNextLevel.setVisibility(4);
tvLevel.setText("" + level);
mPintuLayout.resume();
break;
}
}
3.每一小塊退片的bean
public class ImagePieces {
//小塊圖片的索引值
private int index;
//整個一大塊的圖片載體
private Bitmap bitmap;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
//構造方法
public ImagePieces(int index, Bitmap bitmap) {
super();
this.index = index;
this.bitmap = bitmap;
}
public ImagePieces() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "ImagePieces [index=" + index + ", bitmap=" + bitmap + "]";
}
}
4.圖片分割類
public class ImageSplitterUtil {
//pieces 為切成的塊數,用list保存
public static List splitImage(Bitmap bitmap,int pieces){
List imagePieces = new ArrayList();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
//去小的一方每一個快的寬度
int pieceWidth = Math.min(width,height)/pieces;
for(int i=0;i
5.布局類
首先
private void init() {
// 將margin的值的單位轉為dp
mMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
3, getResources().getDisplayMetrics());
mPadding = min(getPaddingLeft(), getPaddingRight(), getPaddingTop(),
getPaddingBottom());
}
其次onmeasure
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 獲得寬度和高度的最小值
mWidth = min(getMeasuredHeight(), getMeasuredWidth());
if (!once) {
// 進行切圖及排序
initBitmap();
// 設置imageview[item]的寬高等屬性
initItem();
// 標識已畫,防止再畫
once = true;
countTimeBaseLevel();
}
// 寬度和高度的最小值設為寬高
setMeasuredDimension(mWidth, mWidth);
}
其次
// 進行切圖及排序
private void initBitmap() {
if (mBitmap == null) {
mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.psb);
}
mItemBitmaps = ImageSplitterUtil.splitImage(mBitmap, mColumn);
// 是用sort把小塊的圖片亂序
Collections.sort(mItemBitmaps, new Comparator() {
@Override
public int compare(ImagePieces a, ImagePieces b) {
return Math.random() > 0.5 ? 1 : -1;
}
});
}
private int mTime;
// 相對關卡的時間數
private void countTimeBaseLevel() {
mTime = (int) Math.pow(2, mLevel) * 30;
mHandler.sendEmptyMessage(TIME_CHANGED);
}
// 動畫層的布局
private RelativeLayout mAnimaLayout;
private boolean isAniming;
回調接口
public interface GamePintuListener {
void nextLevel(int nextLevel);
void timechanged(int currentTime);
void gameover();
}
public GamePintuListener mListener;
// 設置接口回調
public void setOnGamePintuListener(GamePintuListener mListener) {
this.mListener = mListener;
}
游戲的進程控制,這裡利用的是hangler
private static final int TIME_CHANGED = 0x110;
public static final int NEXT_LEVEL = 0x111;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case TIME_CHANGED:
if (isGameSuccess || isGameOver || isPausing)
return;
if (mListener != null) {
mListener.timechanged(mTime);
if (mTime == 0) {
isGameOver = true;
mListener.gameover();
return;
}
}
mTime--;
mHandler.sendEmptyMessageDelayed(TIME_CHANGED, 1000);
break;
case NEXT_LEVEL:
mLevel = mLevel + 1;
if (mListener != null) {
mListener.nextLevel(mLevel);
} else {
nextLevel();
}
break;
}
}
};
重新開始游戲方法
public void restart() {
isGameOver = false;
mColumn--;
nextLevel();
}
暫停和繼續的方法
private boolean isPausing;
public void pause() {
isPausing = true;
mHandler.removeMessages(TIME_CHANGED);
}
public void resume() {
if (isPausing) {
isPausing = false;
mHandler.sendEmptyMessage(TIME_CHANGED);
}
}
public void nextLevel() {
this.removeAllViews();
mAnimaLayout = null;
mColumn++;
isGameSuccess = false;
countTimeBaseLevel();
initBitmap();
initItem();
};
// 設置imageview[item]的寬高等屬性
private void initItem() {
// 把內邊距和外面距剪掉除以列數就是每一塊的寬度
mItemWidth = (mWidth - mPadding * 2 - mMargin * (mColumn - 1))
/ mColumn;
// item圖片位置的初始化
mPintuItems = new ImageView[mColumn * mColumn];
// item圖片的初始化
for (int i = 0; i < mPintuItems.length; i++) {
ImageView item = new ImageView(getContext());
item.setOnClickListener(this);
item.setImageBitmap(mItemBitmaps.get(i).getBitmap());
mPintuItems[i] = item;
item.setId(i + 1);
// 在item裡面存放index,當拼圖成功時候做為判斷一句
item.setTag(i + "_" + mItemBitmaps.get(i).getIndex());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
mItemWidth, mItemWidth);
// 設置間隙
// 如果不是最後一列
if ((i + 1) % mColumn != 0) {
lp.rightMargin = mMargin;
}
// 不是第一列
if (i % mColumn != 0) {
lp.addRule(RelativeLayout.RIGHT_OF, mPintuItems[i - 1].getId());
}
// 縱向間隙
// 如果不是第一行,設置topMargin和rule
if ((i + 1) > mColumn) {
lp.topMargin = mMargin;
lp.addRule(RelativeLayout.BELOW,
mPintuItems[i - mColumn].getId());
}
addView(item, lp);
}
}
交換圖片
private ImageView mFirst;
private ImageView mSecond;
@Override
public void onClick(View v) {
if (isAniming)
return;
if (mFirst == v) {
mFirst.setColorFilter(null);
mFirst = null;
return;
}
if (mFirst == null) {
mFirst = (ImageView) v;
mFirst.setColorFilter(Color.parseColor("#45f0f0f0"));
} else {
mSecond = (ImageView) v;
exchangView();
}
}
交換圖片的方法exchangeView方法的具體實現
// 交換image
private void exchangView() {
mFirst.setColorFilter(null);
String firstTag = (String) mFirst.getTag();
String secondTag = (String) mSecond.getTag();
String[] firstParams = firstTag.split("_");
String[] secondParams = secondTag.split("_");
final Bitmap firstBitmap = mItemBitmaps.get(
Integer.parseInt(firstParams[0])).getBitmap();
final Bitmap secondBitmap = mItemBitmaps.get(
Integer.parseInt(secondParams[0])).getBitmap();
//設置動畫層,下面有具體實現
setUpAnimLayout();
ImageView first = new ImageView(getContext());
first.setImageBitmap(firstBitmap);
LayoutParams lp = new LayoutParams(mItemWidth, mItemWidth);
lp.leftMargin = mFirst.getLeft() - mPadding;
lp.topMargin = mFirst.getTop() - mPadding;
first.setLayoutParams(lp);
mAnimaLayout.addView(first);
ImageView second = new ImageView(getContext());
second.setImageBitmap(secondBitmap);
LayoutParams lp2 = new LayoutParams(mItemWidth, mItemWidth);
lp2.leftMargin = mSecond.getLeft() - mPadding;
lp2.topMargin = mSecond.getTop() - mPadding;
second.setLayoutParams(lp2);
mAnimaLayout.addView(second);
// 設置動畫
TranslateAnimation anim = new TranslateAnimation(0, mSecond.getLeft()
- mFirst.getLeft(), 0, mSecond.getTop() - mFirst.getTop());
anim.setDuration(300);
anim.setFillAfter(true);
first.setAnimation(anim);
TranslateAnimation anim2 = new TranslateAnimation(0, mFirst.getLeft()
- mSecond.getLeft(), 0, mFirst.getTop() - mSecond.getTop());
anim2.setDuration(300);
anim2.setFillAfter(true);
second.setAnimation(anim2);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
mFirst.setVisibility(View.INVISIBLE);
mSecond.setVisibility(View.INVISIBLE);
isAniming = true;
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
mSecond.setImageBitmap(firstBitmap);
mFirst.setImageBitmap(secondBitmap);
String firstTag = (String) mFirst.getTag();
String secondTag = (String) mSecond.getTag();
mFirst.setTag(secondTag);
mSecond.setTag(firstTag);
mFirst.setVisibility(View.VISIBLE);
mSecond.setVisibility(View.VISIBLE);
mFirst = mSecond = null;
mAnimaLayout.removeAllViews();
// 每次移動完成判斷是否成功
checkSuccess();
isAniming = false;
}
private void checkSuccess() {
boolean isSuccess = true;
for (int i = 0; i < mPintuItems.length; i++) {
ImageView imageview = mPintuItems[i];
String[] tag = imageview.getTag().toString().split("_");
if (Integer.parseInt(tag[1]) != i) {
isSuccess = false;
}
}
if (isSuccess) {
isSuccess = true;
mHandler.removeMessages(TIME_CHANGED);
Toast.makeText(getContext(), "level up!",
Toast.LENGTH_SHORT).show();
mHandler.sendEmptyMessage(NEXT_LEVEL);
}
}
});
}
動畫層的具體實現
private void setUpAnimLayout() {
if (mAnimaLayout == null) {
mAnimaLayout = new RelativeLayout(getContext());
addView(mAnimaLayout);
}
}
// 獲取多個參數的最少值作為內邊距
private int min(int... params) {
int min = params[0];
for (int param : params) {
if (param < min) {
min = param;
}
}
return min;
}
TableLayout經常用到的屬性有:android:collapseColumns:以第0行為序,隱藏指定的列:android:collapseColumns該屬性為
6.0 運行時權限處理在6.0以前 權限都是在安裝時授權的,如果用戶不授權就無法安裝;Android從6.0(API 23)開始 使用運行時權限,而不是像以前那樣安裝時授
在項目開發中,帶刪除按鈕輸入框也是人們常常用到的,該文章便介紹一下如何創建一個帶刪除輸入框。其中,需要解決的問題如下:a)創建自定義editText類b)在自定義edit
SlidingMenu——方式11、初始化SlidingMenu對象2、設置SlidingMenu的菜單模式。 只有左側菜單,只有右側菜單,或者雙