android项目 之 记事本(13) ----- 查看图片及播放录音
本文是自己学习所做笔记,欢迎转载。但请注明出处:http://blog.csdn.net/jesson20121020
今天就来实现下查看图片及录音的功能,在编辑或者浏览记事时,点击图片。打开一个自己定义Activity(当然了,也能够调用系统的图库来查看)来查看所加入的图片的原始图片。而不是缩放后的图片,同理,用自己定义Activity来查看录音文件。实现播放录音的功能。
上图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamVzc29uMjAxMjEwMjA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamVzc29uMjAxMjEwMjA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
从图中也能够看出,我们首先要创建两个Activity。当然了,布局文件也是少不了的。例如以下:
activity_show_picture.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <ImageView
android:id="@+id/iv_showPic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
/> </LinearLayout>
ShowPicture.java
public class ShowPicture extends Activity {
private ImageView img; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_show_picture);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_add);
//设置标题
TextView tv_title = (TextView)findViewById(R.id.tv_title);
tv_title.setText("查看图片");
Button bt_back = (Button)findViewById(R.id.bt_back);
bt_back.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
ShowPicture.this.finish();
}
});
Button bt_del = (Button)findViewById(R.id.bt_save);
bt_del.setBackgroundResource(R.drawable.paint_icon_delete); img = (ImageView)findViewById(R.id.iv_showPic); Intent intent = this.getIntent();
String imgPath = intent.getStringExtra("imgPath");
Bitmap bm = BitmapFactory.decodeFile(imgPath);
img.setImageBitmap(bm);
}
}
主要思想就是用ImageView来显示指定路径的图片。该路径是从前一个Activity中传入进来的。
这里的监听事件。仅仅实现了返回的功能,至于,放大缩小图片。旋转图片。下节再实现吧。activity_show_record.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_centerInParent="true"
> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_margin="5dp"
>
<ImageView
android:id="@+id/iv_record_wave_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_margin="5dp"
android:background="@anim/record_wave_left"
/>
<ImageView
android:id="@+id/iv_microphone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/record_microphone_icon"
android:layout_margin="5dp"
/>
<ImageView
android:id="@+id/iv_record_wave_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_margin="5dp"
android:background="@anim/record_wave_right"
/> </LinearLayout>
<TextView
android:id="@+id/tv_recordTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#499df7"
android:textSize="20sp"
android:text="00:00:00"
android:gravity="center"
android:layout_margin="5dp"
/>
</LinearLayout> </RelativeLayout>
ShowRecord.java
public class ShowRecord extends Activity { private String audioPath;
private int isPlaying = 0;
private AnimationDrawable ad_left,ad_right;
private Timer mTimer;
//语音操作对象
private MediaPlayer mPlayer = null;
private ImageView iv_record_wave_left,iv_record_wave_right,iv_microphone;
private TextView tv_recordTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_show_record);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_add);
//设置标题
TextView tv_title = (TextView)findViewById(R.id.tv_title);
tv_title.setText("查看录音");
Button bt_back = (Button)findViewById(R.id.bt_back);
bt_back.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
if(isPlaying == 1){
mPlayer.stop();
mPlayer.release();
}
ShowRecord.this.finish();
}
});
Button bt_del = (Button)findViewById(R.id.bt_save);
bt_del.setBackgroundResource(R.drawable.paint_icon_delete); Intent intent = this.getIntent();
audioPath = intent.getStringExtra("audioPath"); iv_microphone = (ImageView)findViewById(R.id.iv_microphone);
iv_microphone.setOnClickListener(new ClickEvent()); iv_record_wave_left = (ImageView)findViewById(R.id.iv_record_wave_left);
iv_record_wave_right = (ImageView)findViewById(R.id.iv_record_wave_right); ad_left = (AnimationDrawable)iv_record_wave_left.getBackground();
//ad_left = (AnimationDrawable)iv_record_wave_left.getDrawable();
ad_right = (AnimationDrawable)iv_record_wave_right.getBackground();
//ad_right = (AnimationDrawable)iv_record_wave_right.getDrawable();
tv_recordTime = (TextView)findViewById(R.id.tv_recordTime);
} final Handler handler = new Handler(){
public void handleMessage(Message msg) {
switch(msg.what){
case 1 :
String time[] = tv_recordTime.getText().toString().split(":");
int hour = Integer.parseInt(time[0]);
int minute = Integer.parseInt(time[1]);
int second = Integer.parseInt(time[2]); if(second < 59){
second++; }
else if(second == 59 && minute < 59){
minute++;
second = 0; }
if(second == 59 && minute == 59 && hour < 98){
hour++;
minute = 0;
second = 0;
} time[0] = hour + "";
time[1] = minute + "";
time[2] = second + "";
//调整格式显示到屏幕上
if(second < 10)
time[2] = "0" + second;
if(minute < 10)
time[1] = "0" + minute;
if(hour < 10)
time[0] = "0" + hour; //显示在TextView中
tv_recordTime.setText(time[0]+":"+time[1]+":"+time[2]); break; } }
}; class ClickEvent implements OnClickListener{ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//试听
if(isPlaying == 0){
isPlaying = 1;
mPlayer = new MediaPlayer();
tv_recordTime.setText("00:00:00");
mTimer = new Timer();
mPlayer.setOnCompletionListener(new MediaCompletion());
try {
mPlayer.setDataSource(audioPath);
mPlayer.prepare();
mPlayer.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mTimer.schedule(new TimerTask() { @Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
}, 1000,1000);
//播放动画
ad_left.start();
ad_right.start();
}
//结束试听
else{
isPlaying = 0;
mPlayer.stop();
mPlayer.release();
mPlayer = null;
mTimer.cancel();
mTimer = null;
//停止动画
ad_left.stop();
ad_right.stop();
}
}
} class MediaCompletion implements OnCompletionListener{ @Override
public void onCompletion(MediaPlayer mp) {
mTimer.cancel();
mTimer = null;
isPlaying = 0;
//停止动画
ad_left.stop();
ad_right.stop();
Toast.makeText(ShowRecord.this, "播放完成", Toast.LENGTH_SHORT).show();
tv_recordTime.setText("00:00:00");
}
}
}
在查看录音时,用到了对播放时间的显示处理。以及动画的播放与停止,稍有点复杂,这些在之前“加入录音”一节就就讲述了。
有了这两个Activity后,那么剩下的工作就是在单击图片或者录音的事件中启动这两个Activity就可以。但这就有一个问题,怎样在图文混排的EditText中的推断单击的是图片。录音,还是文字呢??这就须要从EditText中的识别那些是图片。那些是文字,再进一步对图片分析究竟单击的是那一个图片,从而实现查看详细图片及录音的功能,详细例如以下:
1. 记录EditText中每一个图片的位置及所在源路径
为了实如今编辑和浏览时能够随时查看原图片及录音文件,所以在每次加入图片或录音后,用一个List记住新添加的每一个图片或录音的位置及所在路径,当然了,假设是浏览已经存在于数据库中的记事时。在载入数据的同一时候,相同用ListView来记住全部的图片及录音的位置和路径。
主要代码例如以下:
//记录editText中的图片。用于单击时推断单击的是那一个图片
private List<Map<String,String>> imgList = new ArrayList<Map<String,String>>();
每次单击记事列表项。进入查看记事。在载入数据的同一时候将全部图片及录音的位置及路径记录下来。详细为在loadDate()方法中加入下面代码:
//用List记录该录音的位置及所在路径。用于单击事件
Map<String,String> map = new HashMap<String,String>();
map.put("location", m.start()+"-"+m.end());
map.put("path", path);
imgList.add(map);
同理。也要在每次加入图片录音后也要加入对应的代码,在InsertBitmap()函数中加入例如以下代码:
//用List记录该录音的位置及所在路径。用于单击事件
Map<String,String> map = new HashMap<String,String>();
map.put("location", selectionIndex+"-"+(selectionIndex+spannableString.length()));
map.put("path", imgPath);
imgList.add(map);
2. 给EditText加入单击事件
private LineEditText et_Notes; ... ... et_Notes.setOnClickListener(new TextClickEvent());
3. 推断单击的是图片还是普通文字
为了推断单击的是图片还是普通文字,用到了Spanned,ImageSpan。主要思想,就是推断当前单击的位置是否在图片的位置范围内,主要代码例如以下:
Spanned s = et_Notes.getText();
ImageSpan[] imageSpans;
imageSpans = s.getSpans(0, s.length(), ImageSpan.class); int selectionStart = et_Notes.getSelectionStart();
for(ImageSpan span : imageSpans){ int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
//找到图片
if(selectionStart >= start && selectionStart < end){
... ...
} }
打到了图片,接下来就要推断单击的究竟是那一图片呢?
4. 决断单击的详细是那一张图片
这就用到了第一步记录的图片的位置和路径了,显然,就是用位置来推断究竟是单击的那一个图片。主要代码例如以下:
//查找当前单击的图片是哪一个图片
//System.out.println(start+"-----------"+end);
String path = null;
for(int i = 0;i < imgList.size();i++){
Map map = imgList.get(i);
//找到了
if(map.get("location").equals(start+"-"+end)){
path = imgList.get(i).get("path");
break;
}
}
5. 推断是录音还是图片,启动相应的Activity,并传递路径
查看图片,有两种方法,一种是调用系统的图库打开图片,还有一种就是自己定义,这里。我都实现了。打开录音用的是自己定义的Activity,例如以下:
//接着推断当前图片是否是录音。假设为录音,则跳转到试听录音的Activity,假设不是,则跳转到查看图片的界面
//录音,则跳转到试听录音的Activity
if(path.substring(path.length()-3, path.length()).equals("amr")){
Intent intent = new Intent(AddActivity.this,ShowRecord.class);
intent.putExtra("audioPath", path);
startActivity(intent);
}
//图片。则跳转到查看图片的界面
else{
//有两种方法,查看图片。第一种就是直接调用系统的图库查看图片。另外一种是自己定义Activity
//调用系统图库查看图片
/*Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(path);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "image/*");*/
//使用自己定义Activity
Intent intent = new Intent(AddActivity.this,ShowPicture.class);
intent.putExtra("imgPath", path);
startActivity(intent);
}
以上。3,4,5步事实上都是在单击的监听器中实现的。完整代码例如以下:
//为EidtText设置监听器
class TextClickEvent implements OnClickListener{ @Override
public void onClick(View v) {
Spanned s = et_Notes.getText();
ImageSpan[] imageSpans;
imageSpans = s.getSpans(0, s.length(), ImageSpan.class); int selectionStart = et_Notes.getSelectionStart();
for(ImageSpan span : imageSpans){ int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
//找到图片
if(selectionStart >= start && selectionStart < end){
//Bitmap bitmap = ((BitmapDrawable)span.getDrawable()).getBitmap();
//查找当前单击的图片是哪一个图片
//System.out.println(start+"-----------"+end);
String path = null;
for(int i = 0;i < imgList.size();i++){
Map map = imgList.get(i);
//找到了
if(map.get("location").equals(start+"-"+end)){
path = imgList.get(i).get("path");
break;
}
}
//接着推断当前图片是否是录音。假设为录音。则跳转到试听录音的Activity,假设不是,则跳转到查看图片的界面
//录音,则跳转到试听录音的Activity
if(path.substring(path.length()-3, path.length()).equals("amr")){
Intent intent = new Intent(AddActivity.this,ShowRecord.class);
intent.putExtra("audioPath", path);
startActivity(intent);
}
//图片,则跳转到查看图片的界面
else{
//有两种方法,查看图片,第一种就是直接调用系统的图库查看图片,另外一种是自己定义Activity
//调用系统图库查看图片
/*Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(path);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "image/*");*/
//使用自己定义Activity
Intent intent = new Intent(AddActivity.this,ShowPicture.class);
intent.putExtra("imgPath", path);
startActivity(intent);
}
}
else
//假设单击的是空白出或文字。则获得焦点,即打开软键盘
imm.showSoftInput(et_Notes, 0);
}
}
}
至此。就实现了查看图片以及播放录音的功能。
android项目 之 记事本(13) ----- 查看图片及播放录音的更多相关文章
- android项目 之 记事本(11) ----- 加入数据库
本文是自己学习所做笔记.欢迎转载.但请注明出处:http://blog.csdn.net/jesson20121020 通过之前的10节,已实现了记事本的大部分功能,有加入拍照.加入照片,加入录音,加 ...
- android项目 之 记事本(12) ----- 图片的等比例缩放及给图片加入边框
本文是自己学习所做笔记.欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 在Android的UI开发中常常会遇到图片的缩放,就比方记事本,如今的图片都比較 ...
- android项目 之 记事本(6)----- 加入手写
想必大家都用过QQ的白板功能,里面主要有两项,一个是涂鸦功能,事实上类似于上节的画板功能,而还有一个就是手写,那记事本怎么能没有这个功能呢,今天就来为我们的记事本加入手写功能. 先上图,看看效果: 看 ...
- 关于Eclipse创建Android项目时,会多出一个appcompat_v7的问题
问题描述: 使用eclipse创建一个Android项目时,发现project列表中会多创建出一个appcompat_v7项目,再创建一个Android项目时,又会再多出一个appcompat_ ...
- 使用eclipse创建android项目的时候为什么会生成两个项目
使用eclipse创建android项目的时候为什么会生成两个项目 问题描述: 使用eclipse创建一个Android项目时,发现project列表中会多创建出一个appcompat_v7项目,再创 ...
- 【转】关于Eclipse创建Android项目时,会多出一个appcompat_v7的问题
问题描述: 使用eclipse创建一个Android项目时,发现project列表中会多创建出一个appcompat_v7项目,再创建一个Android项目时,又会再多出一个appcompat_v7_ ...
- Android 高仿微信朋友圈动态, 支持双击手势放大并滑动查看图片。
转载请注明出处:http://blog.csdn.net/sk719887916/article/details/40348873 作者skay: 最近参与了开发一款旅行APP,其中包含实时聊天和动态 ...
- 如何在使用eclipse的情况下,清理android项目中的冗余class文件和资源文件以及冗余图片
在我们迭代项目的过程中,经常会启用某些功能,或者修改某些界面的问题,那么问题来了,这样很容易出现大量的冗余.java文件,冗余资源文件,一些冗余的界面文件等.那么问题既然出现了,那么如何去解决呢,这就 ...
- Android Camera开发系列(下)——自定义Camera实现拍照查看图片等功能
Android Camera开发系列(下)--自定义Camera实现拍照查看图片等功能 Android Camera开发系列(上)--Camera的基本调用与实现拍照功能以及获取拍照图片加载大图片 上 ...
随机推荐
- poj1679 次最小生成树 kruskal(暴力枚举)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- 转 DNS原理及其解析过程【精彩剖析】
DNS原理及其解析过程[精彩剖析] http://369369.blog.51cto.com/319630/812889/ DNS原理及其解析过程 精彩剖析 网络通讯大部分是基于T ...
- linux.backspace乱码(转)
42 linux.backspace乱码 linux环境sqlplus中使用backspace键出现乱码的解决方法2008-04-30 16:32 在linux环境下使用sqlplus,在回删(bac ...
- VMWare虚拟机如何与主机共享文件夹(最容易看懂的讲解)附图~
http://wenku.baidu.com/view/54ab9e19227916888486d776.html 新建好虚拟机并安装好系统后,在编辑虚拟机设置--选项进行以下设置: 点添加 选择你要 ...
- LVDS 数据通道详解 单8 单6
1.1.1 LVDS接口分类 1.1.1.1 单路6bit LVDS 这种接口电路中,采用单路方式传输,每个基色信号采用6位数据,共18位RGB数据,因此 ...
- studio构建错误Element uses-permission#android.permission.ACCESS_NETWORK_STATE at AndroidManifest.xml:38:5-79 dupli
今天在项目构建的时候遇到了如下报错: Element uses-permission#android.permission.ACCESS_NETWORK_STATE at AndroidManifes ...
- 基于UDT connect连接通信以及文件传输--服务端
网上与UDT相关的资料不多,与UDT相关的源码例子更少.最近在接触UDT,也是因为缺少相关的资料,导致学习起来甚感痛苦.下面将我自己这两天弄出来的代码贴出来,希望对在寻找相关资料的童鞋有一定的帮助.与 ...
- 2.OpenStack-安装消息队列服务
安装消息队列服务(安装在控制器上) yum install rabbitmq-server -y systemctl start mariadb.service 配置消息队列服务 systemctl ...
- 右上角X灰化
CMenu* menu = this->GetSystemMenu(FALSE); menu->EnableMenuItem(SC_CLOSE, MF_BYCOMMAND | MF_GRA ...
- 腾讯云通信WebIM事件回调的坑~
最近在开过工作中用到了腾讯IM的功能,由于业务的需要主要使用到了: 1.loginInfo 用户登录,用户信息 2.getRecentContactList 获得最近联系人 3.getLastGrou ...