Resterization栅格化是绘制那些Button,Shape,Path,String,Bitmap等组件最基础的操作。它把那些组件拆分到不同的像素上进行显示。这是一个很费时的操作,GPU的引入就是为了加快栅格化的操作。
CPU负责把UI组件计算成Polygons,Texture纹理,然后交给GPU进行栅格化渲染。
CPU、GPU计算,绘制以及渲染都需要在一帧16ms中完成。
二、减少电量损耗的措施:
有下面一些措施能够显著减少电量的消耗:
//获取android.os.ServiceManager类的对象的getService()方法 Method method=Class.forName("android.os.ServiceManager"). getMethod("getService",String.class); // 获取远程TELEPHONY_SERVICE的IBinder对象的代理 IBinder binder =(IBinder)method.invoke(null, new Object[] {TELEPHONY_SERVICE}); // 将IBinder对象的代理转换为ITelephony对象 ITelephony telephony=ITelephony.Stub.asInterface(binder); //挂断电话 telephony.endCall();参考: http://blog.csdn.net/fengyuzhengfan/article/details/38109597
五、获取wrap_content控件的宽高:
ViewTreeObserver observer = myText.getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { if (hasMeasured == false) { int width = myText.getMeasuredWidth(); int height = myText.getMeasuredHeight(); Log.d(TAG, "width:" + width + ";height:" + height); //获取到宽度和高度后,可用于计算 hasMeasured = true; } return true; } });
六、获取屏幕的宽高:
方法一:使用WindowManager.getDefaultDisplay:
// 获取屏幕宽高(方法1) int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽(像素,如:480px) int screenHeight = getWindowManager().getDefaultDisplay().getHeight(); // 屏幕高(像素,如:800p) Log.d(TAG , "getDefaultDisplay:" + "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight); // 由于getWidth与getHeight已废弃,宜使用下面的方法 Point point = new Point(); getWindowManager().getDefaultDisplay().getSize(point); Log.d(TAG, "getDefaultDisplay:" + "screenWidth_x=" + point.x + "; screenHeight_y=" + point.y);
方法二:使用getDisplayMetrics:
// 获取屏幕密度(方法2) DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); float density = dm.density; // 屏幕密度(像素比例:0.75/1.0/1.5/2.0) int densityDPI = dm.densityDpi; // 屏幕密度(每寸像素:120/160/240/320) float xdpi = dm.xdpi; float ydpi = dm.ydpi; Log.d(TAG , " DisplayMetrics:" + "xdpi=" + xdpi + "; ydpi=" + ydpi); Log.d(TAG , " DisplayMetrics:" + "density=" + density + "; densityDPI=" + densityDPI); int screenWidth = dm.widthPixels; // 屏幕宽(像素,如:480px) int screenHeight = dm.heightPixels; // 屏幕高(像素,如:800px) Log.d(TAG , " DisplayMetrics(111):" + "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
方法三:与方法二类似,获取DisplayMetrics的方式如下:
// 获取屏幕密度(方法3) dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm);
@Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getRawX(); int y = (int) event.getRawY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int deltaX = x - mLastX; int deltaY = y - mLastY; int translationX = (int) getTranslationX() + deltaX; int translationY = (int) getTranslationY() + deltaY; setTranslationX(translationX); setTranslationY(translationY); break; case MotionEvent.ACTION_UP: break; default: break; } mLastX = x; mLastY = y; return super.onTouchEvent(event); }
super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); 使用supportRequestWindowFeature替代requestWindowFeature;
在xml文件中使用:
<?xml version="1.0" encoding="utf-8"?> <android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 支持百分比进行空间分配 --> <TextView android:id="@+id/txt1" android:layout_width="0dp" android:layout_height="0dp" android:text="Text1" app:layout_heightPercent="15.5%" android:gravity="center" android:layout_gravity="center_horizontal" app:layout_widthPercent="30%" android:background="@color/aliceblue" /> <!-- 支持Layout的常规用法--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Heel" android:layout_gravity="center" android:background="@color/aliceblue" android:gravity="center" /> <!-- PercentRelativeLayout的使用 --> <android.support.percent.PercentRelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" > <!-- 支持marginLeftPercent等距离属性 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Realt" android:gravity="center" android:background="@color/aliceblue" app:layout_marginLeftPercent="3%" /> </android.support.percent.PercentRelativeLayout> </android.support.percent.PercentFrameLayout>
十、使用ToolBar的title居中的问题,可以设置ToolBar的标题为空,然后再Bar布局中添加一个TextView使其居中显示即可;
<android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" > <TextView android:id="@+id/toolbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:text="@string/tab_name1" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold" /> </android.support.v7.widget.Toolbar>代码中使用:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(""); setSupportActionBar(toolbar);
public class TestActivity extends AppCompatActivity implements View.OnClickListener{ private final int CAPTURE_TAG = 0; private final int PICTURE_HEIGHT = 500; private final int PICTURE_WIDTH = 500; private ImageView testImg; private String filePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); testImg = (ImageView) findViewById(R.id.testImg); testImg.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.testImg: // 保存到外部存储设备如SD卡中 filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myPicture.jpg"; File imageFile = new File(filePath); Uri imageUri = Uri.fromFile(imageFile); // 调用手机中的Camera Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CAPTURE_TAG); break; default: break; } } // 处理拍照后的结果 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { testImg.setImageBitmap(decodeSampledBitmap(filePath, PICTURE_WIDTH, PICTURE_HEIGHT)); } } // 计算图片压缩比例 private int calculateInSampleSize(BitmapFactory.Options options, int picWidth, int picHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > picHeight || width > picWidth) { int heightRatio = Math.round((float) height/ (float) picHeight); int widthRatio = Math.round((float) width / (float) picWidth); inSampleSize = Math.min(heightRatio, widthRatio); } return inSampleSize; } // 压缩图片 private Bitmap decodeSampledBitmap(String imgFilePath, int picWidth, int picHeight) { // 第一次解析将inJustDecodeBounds设置为true,来获取图片大小,此时返回的bitmap为null final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds= true; Bitmap bitmap = BitmapFactory.decodeFile(imgFilePath, options); // 获得压缩比 options.inSampleSize = calculateInSampleSize(options, picWidth, picHeight); // 解析获得图片 options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(imgFilePath, options); } }
//自定义GridView,重写onMeasure方法,使其失去滑动属性,这样才能嵌套在同样具有滑动属性的ScrollView中了。 public class StaticGridView extends GridView { public StaticGridView(Context context) { super(context); } public StaticGridView(Context context, AttributeSet attrs) { super(context, attrs); } public StaticGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }