| 1 | 与像素密度无关的触控笔势 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 权限 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 代码 | private static final float DISTANCE_DIP = 16.0f; | |||||||||||||||||||||||||||||||||||||||||||||||||
| private static final float PATH_DIP = 40.0f; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // convert dip measurements to pixels | ||||||||||||||||||||||||||||||||||||||||||||||||||
| final float scale = getResources().getDisplayMetrics().density; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| int scaledDistance = (int) (DISTANCE_DIP * scale + 0.5f); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| int scaledPath = (int) (PATH_DIP * scale + 0.5f); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // For more information about touch gestures and screens support, see: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // http://developer.android.com/resources/articles/gestures.html | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // http://developer.android.com/reference/android/gesture/package-summary.html | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // http://developer.android.com/guide/practices/screens_support.html | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 缩放位图以查看大小 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 权限 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 代码 | originalImage = Bitmap.createScaledBitmap( | |||||||||||||||||||||||||||||||||||||||||||||||||
| originalImage, // bitmap to resize | ||||||||||||||||||||||||||||||||||||||||||||||||||
| view.getWidth(), // new width | ||||||||||||||||||||||||||||||||||||||||||||||||||
| view.getHeight(), // new height | ||||||||||||||||||||||||||||||||||||||||||||||||||
| true); // bilinear filtering | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 调整 Widget 大小 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 权限 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 代码 | public void onSizeChanged(int w, int h, int oldW, | |||||||||||||||||||||||||||||||||||||||||||||||||
| int oldH) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Calculate relative sizes at runtime | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // mButton and mButtonBackGround are of type Drawable | ||||||||||||||||||||||||||||||||||||||||||||||||||
| int selfW = mButton.getIntrinsicWidth(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| int selfH = mButton.getIntrinsicHeight(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| int marginX = (w - selfW) / 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| int marginY = (h - selfH) / 2; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mButtonBackground.setBounds(marginX, marginY, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| marginX + selfW, marginY + selfH); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| mButton.setBounds(marginX, marginY, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| marginX + selfW, marginY + selfH); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Implement the measureText method to resize text data, if applicable | ||||||||||||||||||||||||||||||||||||||||||||||||||
| measureText(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 响应 Widget 交互 | |||||||||||||||||||||||||||||||||||||||||||||||||
| 权限 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 代码 | // Add import android.view.View.OnClickListener; statement | |||||||||||||||||||||||||||||||||||||||||||||||||
| // Add import android.widget.Button; statement | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Add import android.view.View; statement | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Button b1 = (Button) findViewById(R.id.your_button_id); // Use this method carefully, it consumes lots of system resources | ||||||||||||||||||||||||||||||||||||||||||||||||||
| b1.setOnClickListener(new OnClickListener() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| public void onClick(View v) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Handle the button click here as you wish | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
Android 常用代码---UI
最新推荐文章于 2025-01-31 03:25:37 发布
本文介绍了Android应用中实现高质量用户界面与交互的方法,包括如何创建与像素密度无关的触控笔势、缩放位图以适应不同屏幕尺寸、动态调整Widget大小以保持良好的视觉效果以及响应Widget的用户交互。
588

被折叠的 条评论
为什么被折叠?



