view 生成 bitmap

在开发短信祝福的时候看到别人写了一个贺卡(Imageview textview(edittext)结合),其实原理很简单就是把整个 view 生成了一个bitmap。

//重要部分
             LinearLayout mCenter = (LinearLayout) findViewById(R.id.bitmap);
             mCenter.buildDrawingCache();
             mCenter.setDrawingCacheEnabled(true);
             Bitmap b1 = mCenter.getDrawingCache();

中心代码:

package com.example.sendmsg;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CardViewActivity extends Activity{

    private ImageView topiamge,bottomimage,answer,main_image,changeimage,changetext;
    private EditText content;
    private LinearLayout sendmsg;
    private LinearLayout mCenter;
    private TextView title;
    private Builder builder;
    private Dialog dialog;


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cardvieww);

        init();

        ActionBar actionBar = getActionBar();  
        actionBar.hide();

    }

    public void init(){
        Intent intent = getIntent();
        String text = intent.getStringExtra("content");

        topiamge = (ImageView) findViewById(R.id.topimage);
        bottomimage = (ImageView) findViewById(R.id.bottomimage);
        content = (EditText) findViewById(R.id.content);
        sendmsg = (LinearLayout) findViewById(R.id.sendmsg);
        mCenter = (LinearLayout) findViewById(R.id.bitmap);

        content.setText(text);
        sendmsg.setOnClickListener(new ButtonClick());

        main_image = (ImageView) findViewById(R.id.main_image);
        title = (TextView) findViewById(R.id.title);
        changeimage = (ImageView) findViewById(R.id.changeimage);
        changetext = (ImageView) findViewById(R.id.changetext);

        main_image.setOnClickListener(new BackOnclcik());
        changeimage.setOnClickListener(new ChangeClick());
        changetext.setOnClickListener(new TextStyleClick());
        //验证
        //answer = (ImageView) findViewById(R.id.answer);
    }



    class ButtonClick implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            //view生成bitmap
             mCenter.buildDrawingCache();
             mCenter.setDrawingCacheEnabled(true);
             Bitmap b1 = mCenter.getDrawingCache();  


             //answer.setImageBitmap(b1);//这里是验证的
            //Toast.makeText(CardViewActivity.this, "OK!", Toast.LENGTH_LONG).show();
        }

    }

全部代码如下:

package com.example.sendmsg;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CardViewActivity extends Activity{

    private ImageView topiamge,bottomimage,answer,main_image,changeimage,changetext;
    private EditText content;
    private LinearLayout sendmsg;
    private LinearLayout mCenter;
    private TextView title;
    private Builder builder;
    private Dialog dialog;


    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cardvieww);

        init();

        ActionBar actionBar = getActionBar();  
        actionBar.hide();

    }

    public void init(){
        Intent intent = getIntent();
        String text = intent.getStringExtra("content");

        topiamge = (ImageView) findViewById(R.id.topimage);
        bottomimage = (ImageView) findViewById(R.id.bottomimage);
        content = (EditText) findViewById(R.id.content);
        sendmsg = (LinearLayout) findViewById(R.id.sendmsg);
        mCenter = (LinearLayout) findViewById(R.id.bitmap);

        content.setText(text);
        sendmsg.setOnClickListener(new ButtonClick());

        main_image = (ImageView) findViewById(R.id.main_image);
        title = (TextView) findViewById(R.id.title);
        changeimage = (ImageView) findViewById(R.id.changeimage);
        changetext = (ImageView) findViewById(R.id.changetext);

        main_image.setOnClickListener(new BackOnclcik());
        changeimage.setOnClickListener(new ChangeClick());
        changetext.setOnClickListener(new TextStyleClick());
        //yanzheng
        //answer = (ImageView) findViewById(R.id.answer);
    }




    class ButtonClick implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             mCenter.buildDrawingCache();
             mCenter.setDrawingCacheEnabled(true);
             Bitmap b1 = mCenter.getDrawingCache();  


             //answer.setImageBitmap(b1);
            //Toast.makeText(CardViewActivity.this, "OK!", Toast.LENGTH_LONG).show();
        }

    }

    class BackOnclcik implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            finish();
        }

    }

    class ChangeClick implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            topiamge.setBackgroundResource(R.drawable.top1);
            bottomimage.setBackgroundResource(R.drawable.bottom2);

        }

    }

//  class ChangeTextClick implements OnClickListener{
//
//      @Override
//      public void onClick(View arg0) {
//          // TODO Auto-generated method stub
//          Typeface face = Typeface.createFromAsset (getAssets(), "fonts/fzybxs.TTF" ); 
//          content.setTypeface(face);
//      }
//      
//  }


//一个简单的对话框,改变字体的(忽略不看)
    class TextStyleClick implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            //定义一个AlertDialog.Builder对象  
            builder = new AlertDialog.Builder(CardViewActivity.this);
            LinearLayout loginForm = (LinearLayout)getLayoutInflater().inflate(R.layout.dialogtext,null);
            LinearLayout layout1 = (LinearLayout) loginForm.findViewById(R.id.layout1);
            LinearLayout layout2 = (LinearLayout) loginForm.findViewById(R.id.layout2);
            LinearLayout layout3 = (LinearLayout) loginForm.findViewById(R.id.layout3);
            LinearLayout layout4 = (LinearLayout) loginForm.findViewById(R.id.layout4);


            // 设置对话框显示的View对象  
            builder.setView(loginForm);  

            //创建、并显示对话框  
            builder.create(); 
            dialog = builder.show();

            layout1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Typeface face = Typeface.create("正常", Typeface.NORMAL);
                    content.setTypeface(face);
                    dialog.dismiss();
                }
            });


            layout2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Typeface face = Typeface.createFromAsset (getAssets(), "fonts/ls.ttf" ); 
                    content.setTypeface(face);
                    dialog.dismiss();
                }
            });

            layout3.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Typeface face = Typeface.createFromAsset (getAssets(), "fonts/fzybxs.TTF" ); //字体变换
                    content.setTypeface(face);
                    dialog.dismiss();//点击退出dialog
                }
            });

            layout4.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Typeface face = Typeface.createFromAsset (getAssets(), "fonts/yy.ttf" ); 
                    content.setTypeface(face);
                    dialog.dismiss();
                }
            });

            dialog.show();
        }

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值