创建一个从EditText上与放位图图像;其内容 [英] Create Bitmap Image from EditText & its content

查看:141
本文介绍了创建一个从EditText上与放位图图像;其内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的的EditText 活动里面,我想创建一个位图图片来源的EditText 中的的EditText 的内容。该图片应该是一样的的EditText 内容

这里是我想要的图像看起来应该像,虽然这是一个EditText,但我希望有一个位图看起来应该与此相同。所以,我怎样才能做到这一点?任何想法将是巨大的。

这是我的code,

 公共类EditTextPinchZoomActivity延伸活动{

    的EditText EDITTEXT;
    ImageView的ImageView的;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        ImageView的=(ImageView的)findViewById(R.id.image_view);
        EDITTEXT =(EditText上)findViewById(R.id.edit_text);
        editText.buildDrawingCache();
    }

    公共无效myOnClick(查看视图){

        开关(view.getId()){
        案例R.id.btn:
            imageView.setImageBitmap(editText.getDrawingCache());

            打破;
        默认:
            打破;
        }
    }
}
 

解决方案

您可以通过建立绘图缓存得到位图。这应该工作。

 的EditText编辑=(EditText上)findViewById(R.id.edit);
edit.buildDrawingCache();
ImageView的IMG =(ImageView的)findViewById(R.id.test);
img.setImageBitmap(edit.getDrawingCache());
 

拉​​利特当您尝试建立在onCreate方法缓存,图形还没有发生这样的drawingCache应该什么都没有。要么把在onclick方法buildDrawingChache方法。或者使用下面的code中的onCreate。

  ViewTreeObserver VTO = editText.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
    @覆盖
    公共无效onGlobalLayout(){
        editText.buildDrawingCache();
        }
});
 

不过,我认为构建缓存在onclick方法是正确的作为屏幕届时将绘制。而另一件事我remebered是,如果你的onCreate后输入文本,那么你就应该建立之后缓存。因此,尝试第一种方法。

ViewTreeObserver

其全球布局监听到其不断得到调用时anyhing切换到视图或ViewGroup中。我通常在我需要的宽度和高度的意见在onCreate方法的情况下使用。没有这个值将始终为0,因为没有拉或测量已经发生。

假设你有一个要求,不断更新你的EditText上的ImageView的键入时,您可以使用此,只是删除,我删除了听众的第一道防线。监听器将被调用为每个字符键入即使当光标闪烁。

I have an simple EditText inside an Activity and I want to create a Bitmap Image from that EditText and content of the EditText. The Image should look same as the EditText and its content.

Here is how I want the Image should look like, though this is an EditText but I want a Bitmap which should look same as this. So, how can I achieve this? Any idea would be great.

This is my code,

public class EditTextPinchZoomActivity extends Activity {

    EditText editText;
    ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imageView = (ImageView) findViewById(R.id.image_view);
        editText = (EditText) findViewById(R.id.edit_text);
        editText.buildDrawingCache();
    }

    public void myOnClick(View view) {

        switch (view.getId()) {
        case R.id.btn:
            imageView.setImageBitmap(editText.getDrawingCache());

            break;
        default:
            break;
        }
    }
}

解决方案

You can get the bitmap by building the drawing cache. This should work.

EditText edit = (EditText)findViewById(R.id.edit);
edit.buildDrawingCache();
ImageView img = (ImageView)findViewById(R.id.test);
img.setImageBitmap(edit.getDrawingCache());

Lalit when you try to build the cache in the onCreate method, the drawing hasn't happened yet so the drawingCache should have nothing. Either put the buildDrawingChache method in the onClick method. Or use the following code in onCreate.

ViewTreeObserver vto = editText.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() {
        editText.buildDrawingCache();
        } 
});

But I think building the cache in the onClick method will be proper as the screen will be drawn by then. And other thing I remebered is, if you entering text after the onCreate, then you should build the cache after that. So try the first method.

ViewTreeObserver

Its a global layout listener to a view or viewgroup which keeps getting called when anyhing changes to the layout. I normally use this in cases where I need to get width and height's of views in the onCreate method. Without this the value will always be 0 as no drawing or measuring has taken place.

Suppose you had a requirement to keep updating your imageview of the edittext as you type, you can use this and just remove the first line where I remove the listener. The listener will be called for every character you type even when the cursor blinks.

这篇关于创建一个从EditText上与放位图图像;其内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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