第一个Android简单程序-拼板
16lz
2022-04-02
package zhch.illq;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.PixelFormat;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.LinearLayout;public class PingBanBoard extends Activity { /** Called when the activity is first created. */ Map<Integer, MyButton> buttons = new HashMap<Integer, MyButton>(); private Map<Integer, List<Integer>> relation = new HashMap<Integer, List<Integer>>(); // 图片分的除数 private int colNum = 4; // 当前空白按钮 private int currentBlank = colNum * colNum - 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout mainPanel = (LinearLayout) findViewById(R.id.mainPanel); // 创建放图片的按钮 for (int i = 0; i < colNum; i++) { LinearLayout subLayout = new LinearLayout(this); mainPanel.addView(subLayout); for (int j = 0; j < colNum; j++) { MyButton button = new MyButton(this); buttons.put(i * colNum + j, button); subLayout.addView(button); } } Drawable mm = getResources().getDrawable(R.drawable.mm); buttons.get(1).setBackgroundDrawable(cuteImage(mm, colNum, 1, 1)); // 设置图片 for (int i = 0; i < (colNum * colNum); i++) { buttons.get(i).setBackgroundDrawable(cuteImage(mm, colNum, i % colNum, i / colNum)); android.util.Log.v("number 1", "" + i / colNum + "===" + i % colNum); } // 设置按钮监听器 for (int i = 0; i < (colNum * colNum); i++) { MyButton button = (MyButton) buttons.get(i); button.position = i; button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MyButton view = (MyButton) v; if (moveable(colNum, view.position, currentBlank)) { android.util.Log.v("currentBlank", "" + currentBlank); MyButton blankView = (MyButton) buttons.get(currentBlank); Drawable temp = blankView.getBackground(); blankView.setBackgroundDrawable(view.getBackground()); view.setBackgroundDrawable(temp); currentBlank = view.position; android.util.Log.v("currentBlank ", "" + currentBlank); } } }); } } /** * 取正方形图片的一部分 * * @param drawable * 图片 * @param count * 总行数 * @param row * 行 * @param col * 列 * @return */ public final BitmapDrawable cuteImage(Drawable drawable, int count, int row, int col) { int sideLength = drawable.getIntrinsicWidth(); // 取drawable的边长 // 取小块的边长 int subLength = sideLength / count; Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 取drawable的颜色格式 Bitmap bitmap = Bitmap.createBitmap(sideLength, sideLength, config); // 建立对应bitmap Canvas canvas = new Canvas(bitmap); // 建立对应subBitmap的画布 canvas.clipRect(0, 0, sideLength, sideLength); drawable.setBounds(0, 0, sideLength, sideLength); if ((row + 1) * (col + 1) != count * count) { drawable.draw(canvas); // 把drawable内容画到画布中 } Bitmap subBitmap = Bitmap.createBitmap(bitmap, row * subLength, col * subLength, subLength, subLength);// 取图的一部分 return new BitmapDrawable(subBitmap); } /** * 判断当前位置是否可以移动 * * @param colNum * 分割的阶数 * @param position * 点击的位置 * @param current * 当前空白位置 * @return 是否能移动 */ private boolean moveable(int colNum, int position, int current) { android.util.Log.v("Math.abs(position - current) ", "" + Math.abs(position - current)); android.util.Log.v("Math.max(position, current) ", "" + Math.max(position, current)); if (Math.abs(position - current) == colNum) { return true; } if ((Math.abs(position - current) == 1) && (Math.max(position, current) % colNum != 0)) { return true; } return false; }}
更多相关文章
- android 处理图片工具
- Android自定义view四手势缩放移动的ImageView
- [Android] 调用相机、打开相册、裁剪图片
- Android(安卓)图片加载总结帖
- Android(安卓)--- 图片的特效处理
- 8、RxJava+Retrofit+okhttp上传多张图片给后台
- Android(安卓)工具代码
- Android(安卓)--- 图片的特效处理
- Android从相机或相册获取图片裁剪