Android中View的实时刷新
做一个界面时,想在用户触摸屏幕之后先绘图一下,待逻辑处理完毕,再绘制最后的图,查看View的重绘方法,得知是invalidate()函数,于是在代码中这么写道:
复制代码
运行后发现只有第二次invalidate做了,第一次打酱油去了,于是查找资料,看到Invalidate()的描述是这样的:当调用线程处于空闲状态时,会调用onDraw,刷新界面,也就是说,该函数仅是标记当前界面过期,并不直接负责刷新界面,奶奶的,不刷。。。继续翻啊翻,看到SurfaceView能实现
实时刷新,代码结构如下:
复制代码
这样写好之后,只要在以前调用invalidate()的地方调用repaint()就可以实现
Android View的
实时刷新了。
Android从路径中提取文件名从 Android路径中 提取文件名,有许多种方法,下面介绍两种比较好的方法。
方法一: 利用String类
复制代码
方法二:
利用正则表达式
复制代码
Android一些实用的函数
1:获得屏幕的密度,用于屏幕适配
复制代码
2:
获得版本的名字
复制代码
3:
获得图片的倒影,同时倒影渐变效果
做一个界面时,想在用户触摸屏幕之后先绘图一下,待逻辑处理完毕,再绘制最后的图,查看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);
- }