当前位置: 编程技术>移动开发
本页文章导读:
▪将View的内容投射成Bitmap转图片导出 将View的内容映射成Bitmap转图片导出
将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:在An.........
▪ Animation使用方法(3)简单帧动画 Animation使用方法(三)简单帧动画
工程结构图:[img][/img]main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:.........
▪ 《Maven 实战》读书笔记(6) 聚合 《Maven 实战》读书笔记(六) 聚合
1. Maven聚合的概念聚合概念是由来已久,比如我们需要2个项目协调合作才能完成一个大的、完整的业务场景,这个时候就需要构建2个项目,但.........
[1]将View的内容投射成Bitmap转图片导出
来源: 互联网 发布时间: 2014-02-18
将View的内容映射成Bitmap转图片导出
将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:
在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap。
在使用的时候调用
Bitmap bitmap = view.getDrawingCache();
就可以得到图片的bitmap了。
为了测试这个功能,作者使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。
工程结构图:
[img]
[/img]
布局文件:
main.xml
add_view.xml
set_view.xml
AddViewActivity
MainActivity
SetViewActivity
转自:http://hddev.blog.51cto.com/3365350/629808
只为学习。
将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:
在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap。
在使用的时候调用
Bitmap bitmap = view.getDrawingCache();
就可以得到图片的bitmap了。
为了测试这个功能,作者使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。
工程结构图:
[img]
[/img]
布局文件:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/setview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SET_VIEW" /> <Button android:id="@+id/addview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ADD_VIEW" /> </LinearLayout>
add_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="add_view" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/addViewContent" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/addViewCache" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/imgAddViewCache" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
set_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="set_view" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/content" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/imgSource1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imgSource2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/cache" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/imgCache" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
AddViewActivity
package com.zart;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class AddViewActivity extends Activity {
private LinearLayout addViewContent;
private ImageView imgAddViewCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.add_view);
addViewContent = (LinearLayout) findViewById(R.id.addViewContent);
imgAddViewCache = (ImageView) findViewById(R.id.imgAddViewCache);
// addImgSource();
addRelativeLayout();
addViewContent.setDrawingCacheEnabled(true);
addViewContent.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
addViewContent.layout(0, 0, addViewContent.getMeasuredWidth(),
addViewContent.getMeasuredHeight());
addViewContent.buildDrawingCache();
int color = addViewContent.getDrawingCacheBackgroundColor();
Bitmap cacheBitmap = addViewContent.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);// 注意:这地方必须特别注意
if (bitmap != null) {
imgAddViewCache.setImageBitmap(bitmap);
imgAddViewCache.setDrawingCacheBackgroundColor(color);
} else {
Log.i("CACHE_BITMAP", "DrawingCache=null");
}
}
private void addRelativeLayout() {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams layoutpare = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(layoutpare);
ImageView imgView1 = new ImageView(this);
ImageView imgView2 = new ImageView(this);
imgView1.setImageResource(R.drawable.source1);
imgView2.setImageResource(R.drawable.source2);
RelativeLayout.LayoutParams img1 = new RelativeLayout.LayoutParams(38,
38);
img1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams img2 = new RelativeLayout.LayoutParams(38,
38);
img2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
relativeLayout.addView(imgView1, img1);
relativeLayout.addView(imgView2, img2);
addViewContent.addView(relativeLayout);
}
/**
* 添加图片源
*/
private void addImgSource() {
ImageView imgView1 = new ImageView(this);
ImageView imgView2 = new ImageView(this);
imgView1.setImageResource(R.drawable.source1);
imgView2.setImageResource(R.drawable.source2);
addViewContent.addView(imgView1, new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
addViewContent.addView(imgView2, new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
MainActivity
package com.zart;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button btn_setView;
private Button btn_addView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_setView = (Button) findViewById(R.id.setview);
btn_addView = (Button) findViewById(R.id.addview);
btn_setView.setOnClickListener(this);
btn_addView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
switch (view.getId()) {
case R.id.setview:
Intent intent1 = new Intent();
intent1.setClass(this, SetViewActivity.class);
startActivity(intent1);
break;
case R.id.addview:
Intent intent2 = new Intent();
intent2.setClass(this, AddViewActivity.class);
startActivity(intent2);
break;
default:
break;
}
}
}
SetViewActivity
package com.zart;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View.MeasureSpec;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class SetViewActivity extends Activity {
/** Called when the activity is first created. */
private LinearLayout contentLayout;
private ImageView imgSource1;
private ImageView imgSource2;
private ImageView imgCache;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.set_view);
contentLayout = (LinearLayout) findViewById(R.id.content);
imgSource1 = (ImageView) findViewById(R.id.imgSource1);
imgSource2 = (ImageView) findViewById(R.id.imgSource2);
imgCache = (ImageView) findViewById(R.id.imgCache);
imgSource1.setImageResource(R.drawable.source1);
imgSource2.setImageResource(R.drawable.source2);
contentLayout.setDrawingCacheEnabled(true);
contentLayout.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),
contentLayout.getMeasuredHeight());
contentLayout.buildDrawingCache();
Bitmap bitmap= contentLayout.getDrawingCache();
if(bitmap!=null){
imgCache.setImageBitmap(bitmap);
}else{
Log.i("CACHE_BITMAP", "DrawingCache=null");
}
}
}转自:http://hddev.blog.51cto.com/3365350/629808
只为学习。
[2] Animation使用方法(3)简单帧动画
来源: 互联网 发布时间: 2014-02-18
Animation使用方法(三)简单帧动画
工程结构图:
[img]
[/img]
main.xml
/res/drawable-hdpi/move.xml
AnimationDemo5Activity
工程结构图:
[img]
[/img]
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
></Button>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
/>
</LinearLayout>
/res/drawable-hdpi/move.xml
<?xml version="1.0" encoding="UTF-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/p1" android:duration="500"></item> <item android:drawable="@drawable/p2" android:duration="500"></item> <item android:drawable="@drawable/p3" android:duration="500"></item> <item android:drawable="@drawable/p4" android:duration="500"></item> </animation-list>
AnimationDemo5Activity
package cxt.demo;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class AnimationDemo5Activity extends Activity {
/** Called when the activity is first created. */
Button start = null;
ImageView imageView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.image);
start = (Button)findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
imageView.setBackgroundResource(R.drawable.move);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
animationDrawable.start();
}
});
}
}
[3] 《Maven 实战》读书笔记(6) 聚合
来源: 互联网 发布时间: 2014-02-18
《Maven 实战》读书笔记(六) 聚合
1. Maven聚合的概念
聚合概念是由来已久,比如我们需要2个项目协调合作才能完成一个大的、完整的业务场景,这个时候就需要构建2个项目,但是呢,想一次性就构建这2个项目,而不是分别构建2个项目后再合在一起整。这样我们就需要一个比较特殊的项目了,就是所谓的聚合项目,这个项目没有别的目的,就是仅仅为了聚合多个项目模块用的。
2. 新的项目模块
在介绍聚合项目前,先利用IDE建立一个新的项目模块——MavenAccount-persist,负责注册系统的持久层业务,按照Maven的项目规范,我们建立项目如下。
实体POJO如下
业务接口
接口实现类
之前咱们说过,Java具体技术不是我们这个学习笔记系列的重点,所以我只是输出一些简单信息,并没有和任何的持久层框架结合起来。咱们权且当做完成了这个注册系统的持久层模块吧。
Spring配置文件
之后两个模块就一起构建了。
4. 总结
我们这里仅仅介绍了集合模块一起构建的方式,当然如果模块少,一个一个分别build成功后再集成也无可厚非,看项目具体情况而定。这次我们留了一个问题,就是这两个项目模块使用的依赖包版本不一致怎么办,因为pom.xml是分别声明的依赖,这个问题我们留在了Maven继承机制解决。
1. Maven聚合的概念
聚合概念是由来已久,比如我们需要2个项目协调合作才能完成一个大的、完整的业务场景,这个时候就需要构建2个项目,但是呢,想一次性就构建这2个项目,而不是分别构建2个项目后再合在一起整。这样我们就需要一个比较特殊的项目了,就是所谓的聚合项目,这个项目没有别的目的,就是仅仅为了聚合多个项目模块用的。
2. 新的项目模块
在介绍聚合项目前,先利用IDE建立一个新的项目模块——MavenAccount-persist,负责注册系统的持久层业务,按照Maven的项目规范,我们建立项目如下。
实体POJO如下
package pojo;
/**
* 注册用户实体
* @author liuyan
*/
public class Account {
private String id;
private String name;
private String password;
private String email;
//省去setter和getter
} 业务接口
package dao;
import pojo.Account;
/**
* 业务接口
* @author liuyan
*/
public interface AccountDAO {
public boolean save(Account account);
public boolean update(Account account);
public Account query();
public boolean delete(String id);
} 接口实现类
package dao.impl;
import pojo.Account;
import dao.AccountDAO;
/**
* 业务实现类
* @author liuyan
*/
public class AccountDAOImpl implements AccountDAO {
@Override
public boolean delete(String id) {
System.out.println("删除记录");
return true;
}
@Override
public Account query() {
System.out.println("查找记录");
return null;
}
@Override
public boolean save(Account account) {
System.out.println("保存记录");
return true;
}
@Override
public boolean update(Account account) {
System.out.println("更新记录");
return true;
}
} 之前咱们说过,Java具体技术不是我们这个学习笔记系列的重点,所以我只是输出一些简单信息,并没有和任何的持久层框架结合起来。咱们权且当做完成了这个注册系统的持久层模块吧。
Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p">
<bean id="accountDAO" >
</bean>
</beans> mvn clean install
之后两个模块就一起构建了。
[INFO] ------------------------------------
[INFO] Building MavenAccount-aggregator 0.0.1-SNAPSHOT
[INFO] ------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ MavenAccount-aggrega
tor ---
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ MavenAccount-a
ggregator ---
[INFO] Installing E:\Genuitec\ws\MavenAccount-aggregator\pom.xml to C:\Users\liu
yan\.m2\repository\com\liuyan\account\MavenAccount-aggregator\0.0.1-SNAPSHOT\Mav
enAccount-aggregator-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] MavenAccount-email ................................ SUCCESS [6.553s]
[INFO] MavenAccount-persist .............................. SUCCESS [1.498s]
[INFO] MavenAccount-aggregator ........................... SUCCESS [0.062s]
[INFO] ------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------
[INFO] Total time: 8.300s
[INFO] Finished at: Sun Jun 05 12:51:01 CST 2011
[INFO] Final Memory: 13M/31M
[INFO] ------------------------------------ 4. 总结
我们这里仅仅介绍了集合模块一起构建的方式,当然如果模块少,一个一个分别build成功后再集成也无可厚非,看项目具体情况而定。这次我们留了一个问题,就是这两个项目模块使用的依赖包版本不一致怎么办,因为pom.xml是分别声明的依赖,这个问题我们留在了Maven继承机制解决。
最新技术文章: