常用方法
- 1 bitmap.setPixel(int x, int y, int color)
- 2 relativelayout.layoutParams.width 为 -1
1 bitmap.setPixels()
作用:为bitmap的某个像素赋值为某个颜色。 x y表示改像素点在图片中的坐标值, color表示ARGB颜色。
demo:
// 从图片左上角开始,向右移100像素,向下移50像素,
// 将该像素点的颜色设置为red。
int red = getColor(R.color.red);
bitmap.setPixel(100,50, red);
2 relativelayout.layoutParams.width 为 -1
在Relativelayout中,如果我们设置了 子view的宽度为 match_parent.那么我们在代码中使用子view 获取其layoutParams.width的时候,获取的值是 -1;如果设置了子view的宽度为 wrap_content.则 layoutParams.width 为-2;
demo:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World2!" />
<TextView
android:id="@+id/tv_3"
android:layout_width="100dp"
android:layout_height="200dp"
android:text="Hello World2!" />
</RelativeLayout>
我们在activity中打印获取宽度的log:
Log.i(TAG,
" tv_1 width: " + tv_1.getLayoutParams().width +
" tv_2 width: " + tv_2.getLayoutParams().width +
" tv_3 width: " + tv_3.getLayoutParams().width
);
输出的结果为:
2020-12-31 17:54:49.624 28853-28853/com.demotest I/MainActivity: tv_1 width: -2 tv_2 width: -1 tv_3 width: 350
所以,我们在获取父view为 Relativelayout的LayoutParams的时候,要特别注意。