智能管家项目总结
一、难点
一、应用全屏问题
-
状态栏的隐藏;
-
导航条的隐藏;
-
沉浸式模式;
二、 聊天界面
-
关于适配器里面的几个方法
@Override public int getItemViewType(int position) { return list.get(position).getType(); } //解决两个ViewHolder转换报错 //解释有多少个View @Override public int getViewTypeCount() { return 3; } //让每一个item不可点击 @Override public boolean isEnabled(int position) { return false; }
-
当界面切换时数据被清空
-
原因:使用ViewPager加载Fragment时,ViewPager会默认加载前一个页面和后一个一面,如果通过单击title进行切换页面也只是加载前一个和后一个,当ViewPager切换到第三个页面的时候,第一个fragment就会调用onDestroy()方法,所以数据就被清空了;
-
解决:
-
通过ViewPager的setOffscreenPageLimit(int i)直接设置ViewPager的预加载个数,数据太多就会卡;
-
在Fragemnt的onSaveInstanceState()方法中保存数据,下一次加载fragemnt的时候直接取出即可;(保存List< Obiect > 方法:对Object进行序列化,即实现Serializable);在取出来的时候强制类型转化会有黄色警告:
解决:
Serializable serializable = savedInstanceState.getSerializable("list"); if (serializable instanceof ArrayList<?>){ ArrayList<?> lists = (ArrayList<?>) serializable; for (Object o : lists) { list.add((ButlerChart) o); } } ```
-
-
三、新闻界面
-
使用WebView加载网页问题:
private void loadUrl(){ WebSettings settings = webView_news.getSettings(); settings.setJavaScriptEnabled(true); settings.setSupportZoom(true); settings.setDisplayZoomControls(true); settings.setBuiltInZoomControls(true); webView_news.loadUrl(url); //监听进度 webView_news.setWebChromeClient(new MyWebViewClient()); //不跳转到浏览器 webView_news.setWebViewClient(new WebViewClient()); } public class MyWebViewClient extends WebChromeClient{ @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); Log.d(TAG, "onProgressChanged: "+newProgress); if (newProgress == 100){ progress.setVisibility(View.GONE); } } }
四、图片社区
-
以对话框的形式放大图片
- 让导航条和状态栏透明(API21以上,以下直接全屏)
window.setNavigationBarColor(Color.TRANSPARENT); window.setStatusBarColor(Color.TRANSPARENT);
- 使用动画
-
给dialog设置动画:
dialog.getWindow().setWindowAnimations(R.style.PictureMyDialog);
-
给图片设置动画
//获取图片的位置 int[] ints = new int[2]; imageView.getLocationOnScreen(ints); //设置图片缩放动画 ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1, ints[0], ints[1]); scaleAnimation.setDuration(500); PhotoView photoView = inflate.findViewById(R.id.imageView); photoView.setAnimation(scaleAnimation); Glide.with(inflate).load(url).into(photoView);
-
五、用户信息
-
获取手机里的图片(SD卡的读写为危险权限)
//用手机里面的图片 private void getPhoto() { Intent intent; if (Build.VERSION.SDK_INT < 19) { intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); } else { intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); } startActivityForResult(intent, StaticClass.REQUEST_CODE_PHOTO); } //回调 @Override public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case StaticClass.REQUEST_CODE_PHOTO: if (resultCode == RESULT_OK) if (null != data) { // data.getExtras();null changePhoto(data.getData()); } break; case StaticClass.REQUEST_CODE_CAMERA: if (resultCode == RESULT_OK) { // changePhoto(data); changePhoto(photoUri); } break; } }
-
调用照相机(照相机权限为危险权限需要动态申请)
private Uri photoUri; //拍照 private void tackPhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ContentValues values = new ContentValues(); photoUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Log.d(TAG, "tackPhoto: " + photoUri); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(intent, StaticClass.REQUEST_CODE_CAMERA); }
-
更换头像
//更换头像 private void changePhoto(Uri uri) { Log.d(TAG, "changePhoto: " + uri); if (uri != null) { Cursor query = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null); if (query != null) { query.moveToFirst(); String string = query.getString(query.getColumnIndex(MediaStore.Images.Media.DATA)); query.close(); Log.d(TAG, uri.getPath() + "changePhoto: " + string); Bitmap bitmap = BitmapFactory.decodeFile(string); img_head.setImageBitmap(bitmap); //String[] split = string.split("/"); saveImageToBitmap(img_head); } } }
-
保存头像
//保存头像到本地 //context.getExternalFilesDir(null);获取到项目在sd卡的位置,即: ///storage/emulated/0/Android/data/com.example.along.myfirstapplication/files/ private void saveImageToBitmap(ImageView imageView) { String image = StaticClass.getPath(context, "image"); Log.d(TAG, "saveImageToBitmap: " + image); if (!image.equals("")) { File file = new File(image, "head_img.png"); BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); Bitmap bitmap = drawable.getBitmap(); BufferedOutputStream bos; try { bos = new BufferedOutputStream(new FileOutputStream(file)); bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); bos.flush(); bos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }