做一个界面时,想在用户触摸屏幕之后先绘图一下,待逻辑处理完毕,再绘制最后的图,查看View的重绘方法,得知是invalidate()函数,于是在代码中这么写道:
- public class PuzzleView extends View {
- @Override
- protected void onDraw(Canvas canvas) {
- //...
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- invalidate();
- //处理逻辑
- invalidate();
- }
- }
- public class PuzzleView extends SurfaceView implements SurfaceHolder.Callback{
- private SurfaceHolder surfaceHolder;
-
- public PuzzleView(Context context){
- //....
- surfaceHolder = this.getHolder();//获取holder
- surfaceHolder.addCallback(this);
- }
-
- protected void paint(Canvas canvas) {
- //这里的代码跟继承View时OnDraw中一样
- }
-
- public void repaint() {
- Canvas c = null;
- try {
- c = surfaceHolder.lockCanvas();
- paint(c);
- }
- finally {
- if (c != null) {
- surfaceHolder.unlockCanvasAndPost(c);
- }
- }
- }
- }
Android从路径中提取文件名从 Android路径中 提取文件名,有许多种方法,下面介绍两种比较好的方法。
方法一: 利用String类
- public String getFileName(String pathandname){
- int start=pathandname.lastIndexOf("/");
- int end=pathandname.lastIndexOf(".");
- if (start!=-1 && end!=-1) {
- return pathandname.substring(start+1, end);
- }
- else {
- return null;
- }
- }
- String regEx = ".+\\\\(.+)$";
- String str = "C:\\test.txt";
- Pattern p = Pattern.compile(regEx);
- Matcher m = p.matcher(str);
- boolean rs = m.find();
- if (rs) {
- for(int i=1; i<=m.groupCount(); i++) {
- System.out.println(m.group(i)); //括号内匹配内容
- }
- }
1:获得屏幕的密度,用于屏幕适配
- public static float getDensity(Context ctx) {
- DisplayMetrics metrics = new DisplayMetrics();
- WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
- wm.getDefaultDisplay().getMetrics(metrics);
- return metrics.density;
- }
- public static String getVersionName(Context context, String packageName) {
- PackageInfo pInfo = null;
- String rs = "";
- try {
- pInfo = context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_META_DATA);
- rs = pInfo.versionName;
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- return rs;
- }
- public static Bitmap createMirro(Bitmap srcbitmap) {
-
- int width = srcbitmap.getWidth();
- int height = srcbitmap.getHeight();
- int shadow_height = 15;
- int[] pixels = new int[width * height];
- srcbitmap.getPixels(pixels, 0, width, 0, 0, width, height);
-
- // shadow effect
- int alpha = 0x00000000;
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int index = y * width + x;
- int r = (pixels[index] >> 16) & 0xff;
- int g = (pixels[index] >> 8) & 0xff;
- int b = pixels[index] & 0xff;
- pixels[index] = alpha | (r << 16) | (g << 8) | b;
- }
- if (y >= (height - shadow_height)) {
- alpha = alpha + 0x1F000000;
- }
- }
-
- // invert effect
- Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
- for (int y = 0; y < height; y++) {
- bm.setPixels(pixels, y * width, width, 0, height - y - 1, width, 1);
- }
- return Bitmap.createBitmap(bm, 0, 0, width, shadow_height);
- }