在开发短信祝福的时候看到别人写了一个贺卡(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();
}
}
}