View 中的 ImageView setImageBitmap()
ImageView setImageBitmap() from View
我正在做一些实验,我正在尝试做这样的事情:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyImageView view = new MyImageView(this);
setContentView(view);
}
}
public class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
View view = View.inflate(context, R.layout.my_view, null);
((TextView) view.findViewById(R.id.view_label)).setText(...);
Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
setImageBitmap(bitmap);
}
}
我的 R.layout.my_view
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/blue_spot"
android:gravity="center">
<TextView
tools:text="99"
android:id="@+id/view_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="bold"/>
</LinearLayout>
我的身材:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dp"/>
<solid android:color="#5AB7FF"/>
</shape>
我得到一个空白的空白屏幕...知道为什么吗?
我正在做所有这些解决方法,因为稍后我想使用 myImageView.setImageMatrix()
.
谢谢。
好的,我明白了。它什么也没画,因为那个视图还没有被渲染。我需要的是一种在不绘制视图的情况下渲染该视图(在 canvas 中)然后将该视图绘制到位图的方法。这可以通过以下方式实现:
view.setDrawingCacheEnabled(true);
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
现在我只需要做一些调整,但我已经画好了。
谢谢
我正在做一些实验,我正在尝试做这样的事情:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyImageView view = new MyImageView(this);
setContentView(view);
}
}
public class MyImageView extends ImageView {
public MyImageView(Context context) {
super(context);
View view = View.inflate(context, R.layout.my_view, null);
((TextView) view.findViewById(R.id.view_label)).setText(...);
Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
setImageBitmap(bitmap);
}
}
我的 R.layout.my_view
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/blue_spot"
android:gravity="center">
<TextView
tools:text="99"
android:id="@+id/view_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="bold"/>
</LinearLayout>
我的身材:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<corners android:radius="10dp"/>
<solid android:color="#5AB7FF"/>
</shape>
我得到一个空白的空白屏幕...知道为什么吗?
我正在做所有这些解决方法,因为稍后我想使用 myImageView.setImageMatrix()
.
谢谢。
好的,我明白了。它什么也没画,因为那个视图还没有被渲染。我需要的是一种在不绘制视图的情况下渲染该视图(在 canvas 中)然后将该视图绘制到位图的方法。这可以通过以下方式实现:
view.setDrawingCacheEnabled(true);
view.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
现在我只需要做一些调整,但我已经画好了。
谢谢