一款简易垃圾识别app界面设计
在刚开始的初始界面做完后,这段时间大佬也完成了识别的功能,接着我们就将我们各自的内容结合在一起,在合并的过程中出现了亿点点小问题.就顺便写下来方便学习
显示/返回
在原界面上显示图片太小,显示不清晰,我们另用一个新的界面来显示识别的结果图片,首先要做到的就是新界面的跳转和返回;
新界面布局很简单,线性布局一个ImageButton一个ImageView就完事
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageButton
android:id="@+id/return_main"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#FFFFFF"
android:src="@mipmap/return_main">
</ImageButton>
<ImageView
android:id="@+id/show_image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
按钮添加点击事件返回
/*
点击返回按钮返回主界面
*/
btr=findViewById(R.id.return_main);
btr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
还有记得在AndroidManifest里添加
<activity android:name=".ShowImage" />
不然会闪退
跳转/传递数据
传递数据这一步,刚开始对我来说非常麻烦,因为在以往的activity里采用的传递数据inten在传递数据的时候是有大小限制的,网上有人做过测试,如果是传递Bitmap的话,当图片大小超过1024也就是1M左右的时候,程序就会出现闪退或者停止运行的情况(不同手机情况不同)。
经过查询和实践,我们最后采取了将图片保存到本地,再传递路径uri,并在跳转后将对应路径uri的图片取出的方法。
参考博客Activity之间传递数据
代码段
/*
传递路径并跳转
*/
Intent intent=new Intent(MainActivity.this,ShowImage.class);
Uri imageUri=Uri.parse(Img.savePath);
intent.setData(imageUri);
startActivity(intent);
/*
显示
*/
show=findViewById(R.id.show_image);
Uri uri = Uri.fromFile(Img.filePic);//Img.filePic是File型
image=getBitmapFromUri(uri);//image是Bitmap类型
show.setImageBitmap(image);
//获取Uri中的bitmap
private Bitmap getBitmapFromUri(Uri uri)
{
try
{
// 读取uri资源中的bitmap
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
return bitmap;
}
catch (Exception e)
{
Log.e("[Android]", e.getMessage());
Log.e("[Android]", "目录为:" + uri);
e.printStackTrace();
return null;
}
}
Img是我们自己封装的一个类
public class Img {
public static String savePath;
public static File filePic;
public static void saveBitmap(Bitmap bitmap)
}
基本代码段就是这样,如果后续有修改那也会继续跟进,不过下一次改应该就是最后一版了。