如何检索从视图中绘制(图)? [英] How to retrieve drawable (picture) from a view?

查看:73
本文介绍了如何检索从视图中绘制(图)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了code画上的景色。它完成后,我怎样才能从视图中生成的图像。例如,在下面的code,我想从mCustomDrawableView获得的绘制(图)。我该怎么办呢?谢谢你。

 公共类HelloTestGraph延伸活动{
    / **当第一次创建活动调用。 * /    // @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.main);
        LO的LinearLayout =(的LinearLayout)findViewById(R.id.top_view);
        LinearLayout.LayoutParams参数=新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);        CustomDrawableView mCustomDrawableView =新CustomDrawableView(本);
        mCustomDrawableView.setLayoutParams(参数);
        lo.addView(mCustomDrawableView);
    }    公共类CustomDrawableView扩展视图{
        私人ShapeDrawable mDrawable;
        私人可绘制MPIC;        公共CustomDrawableView(上下文的背景下){
            超级(上下文);            INT X = 10;
            诠释Y = 10;
            INT宽度= 300;
            INT高度= 50;            mDrawable =新ShapeDrawable(新OvalShape());
            。mDrawable.getPaint()的setColor(0xff74AC23);
            mDrawable.setBounds(X,Y,X +宽度,Y +高度);            MPIC = getResources()getDrawable(R.drawable.example_picture)。
            mPic.setBounds(X,Y + 100中,x +宽,Y +高度+ 100);
        }        保护无效的onDraw(帆布油画){
            mDrawable.draw(画布);
            mPic.draw(画布);
        }
    }
}


解决方案

这是一个有点令人费解,但应该得到你。

第1步:创建所需大小的Muteable位图和藏匿它放在一边。这可能是设备的屏幕大小或最大视图大小(取决于你想用它以后做什么)搁置保存的位图指针类似MYBITMAP

第二步:创建与上述位图的画布。 画布上myCanvas =新的Canvas(MYBITMAP);

第3步:在您的onDraw()方法,得出你的观点都在画布传入的对象,并以自己的自定义的

 保护无效的onDraw(帆布油画){
    mDrawable.draw(画布);
    mPic.draw(画布);
    mDrawable.draw(myCanvas);
    mPic.draw(myCanvas);
}

第四步:您的原始位现在应该只包含视图的完全渲染的版本

我不知道如果这正是你要寻找的,但它会给你一个位图的视图的内容(您可以转换成图像)。

I wrote code to draw on a view. After it is done, how can I get the resulting image from the view. For example, in the code below, I want to get the the drawable (Image) from mCustomDrawableView. How can I do that? Thanks.

public class HelloTestGraph extends Activity {
    /** Called when the activity is first created. */

    // @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        LinearLayout lo = (LinearLayout) findViewById(R.id.top_view);
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        CustomDrawableView mCustomDrawableView = new CustomDrawableView(this);
        mCustomDrawableView.setLayoutParams(param);
        lo.addView(mCustomDrawableView);
    }

    public class CustomDrawableView extends View {
        private ShapeDrawable mDrawable;
        private Drawable mPic;

        public CustomDrawableView(Context context) {
            super(context);

            int x = 10;
            int y = 10;
            int width = 300;
            int height = 50;

            mDrawable = new ShapeDrawable(new OvalShape());
            mDrawable.getPaint().setColor(0xff74AC23);
            mDrawable.setBounds(x, y, x + width, y + height);

            mPic = getResources().getDrawable(R.drawable.example_picture);
            mPic.setBounds(x, y + 100, x + width, y + height+100);
        }

        protected void onDraw(Canvas canvas) {
            mDrawable.draw(canvas);
            mPic.draw(canvas);
        }
    }
}

解决方案

This is a little convoluted, but should get you there.

Step 1: Create a Muteable Bitmap of the size you want and stash it aside. It could be the size of the device's screen or the size of your largest view (depending on what you want to do with it later) Save that bitmap pointer aside as something like myBitmap

Step 2: Create a canvas with the aforementioned bitmap. "Canvas myCanvas = new Canvas(myBitmap);"

Step 3: In your onDraw() method, draw your views both to the passed in "canvas" object, and to your own custom one.

protected void onDraw(Canvas canvas) {
    mDrawable.draw(canvas);
    mPic.draw(canvas);
    mDrawable.draw(myCanvas);
    mPic.draw(myCanvas);
}

Step 4: Your original bitmap should now contain the fully rendered version of ONLY your view.

I'm not sure if this is exactly what you're looking for, but it will give you a bitmap (which you can convert into an image) of the contents of your view.

这篇关于如何检索从视图中绘制(图)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆