如何从Android中相对布局的绘制 [英] How to get the drawable from relative layout in android
本文介绍了如何从Android中相对布局的绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我做了什么:
我已经设置了位图的相对布局如下
What i have done: I have set the bitmap for a relative layout as below
RelativeLayout root;
root= (RelativeLayout) itemView.findViewById(R.id.root);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap preview_bitmap = BitmapFactory.decodeResource(context.getResources(),godImages[position], options);
Drawable drawable = new BitmapDrawable(context.getResources(),preview_bitmap);
root.setBackgroundDrawable(drawable);
我试图做的:
我想再次被设置得从RelativeLayout的位图
这样我可以传递到另一个活动。
What i am trying to do: I am trying to get the bitmap from the relativelayout back again that was set so that i can pass to another activity.
推荐答案
有几个方法可以做到这一点:
There are a couple of ways you can do this:
1 尝试:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
layout.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(layout.getDrawingCache());
layout.setDrawingCacheEnabled(false);
2 或尝试:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.rel);
Bitmap b = Bitmap.createBitmap( layout.getLayoutParams().width, layout.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
layout.layout(layout.getLeft(), layout.getTop(), layout.getRight(), layout.getBottom());
layout.draw(c);
要获得当前的ImageView
1 以获得当前位置,用
ViewPager viewPager = getView().findViewById(r.id.pager);
int position = viewPager.getCurrentItem();
2 以获得当前查看
,用
View currentView = viewPager.getChildAt(int position);
本 currentView
是查看
的 onCreateView()$ C创建$ C>的方法当前
片段
。
3 :要获得的ImageView
,用
ImageView currentImage = (ImageView)currentView.findViewById(R.id.image);
其中, R.id.image
是从当前希望的ImageView
对象片段
。
这篇关于如何从Android中相对布局的绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文