Android中实用小知识
16lz
2021-01-23
不断更新中,有好的提议的请评论提示,我会尽快补上。谢谢。
1、Edit Text改变光标的颜色、密码
光标的颜色
EditText有一个属性:android:textCursorDrawable,这个属性是用来控制光标颜色的
android:textCursorDrawable="@null","@null"作用是让光标颜色和text color一样
密码:
android:inputType="textPassword"
Edittext设置取消下划线:background="@null"
RadioButton设置默认选中
textView设置单行过长显示省略
android:singleLine="true"
android:maxWidth="90dp"
android:ellipsize="end" //省略号显示在结尾
android:maxEms="11" //最多显示几个字符
2、动态加载View
第一种
LinearLayout ll = (LinearLayout)findViewById(R.id.viewObj);// 将TextView 加入到LinearLayout 中TextView tv = new TextView(this);tv.setText(Hello World);ll. addView ( tv );
第二种
//在布局中新建一个相对布局 RelativeLayout relative = new RelativeLayout(MainActivity.this); //设置背景颜色 relative.setBackgroundColor(Color.BLUE); //设置布局的长宽,如果父布局是LinearLayout,就用LinearLayout.LayoutParams LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //设置Margin 参数:左上右下 params.setMargins(80,10,10,10); //将参数填入相对布局中 relative.setLayoutParams(params); //在相对布局中添加一个textView TextView tv = new TextView(this); //textView 的参数,因为父布局是相对布局,所以用RelativeLayout.Params RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); //margin参数 p.setMargins(100,0,0,0); //设置textView在RelativeLayout中的位置 p.addRule(RelativeLayout.CENTER_IN_PARENT); //写入参数 tv.setLayoutParams(p); //设置文字 tv.setText("ZJH"); //设置文字颜色 tv.setTextColor(Color.WHITE); //将textView添加到相对布局中 relative.addView(tv); //将相对布局添加到最外层的LinearLayout中 count.addView(relative);
3、调用系统相机
//调用相机 File mPhotoFile = new File(folder,filename); Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri fileUri = Uri.fromFile(mPhotoFile); captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); mActivity.startActivityForResult(captureIntent, CAPTURE_PHOTO_REQUEST_CODE);
/** * 判断系统中是否存在可以启动的相机应用 * * @return 存在返回true,不存在返回false */ public boolean hasCamera() { PackageManager packageManager = mActivity.getPackageManager(); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); return list.size() > 0; }
4、调用相册
//中拿到照片 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CapturePhotoHelper.CAPTURE_PHOTO_REQUEST_CODE & resultCode == RESULT_OK) { File photoFile = mCapturePhotoHelper.getPhoto();//获取拍完的照片 if (photoFile != null) { PhotoPreviewActivity.preview(this, photoFile);//跳转到预览界面 } finish(); } else { super.onActivityResult(requestCode, resultCode, data); } }
/** * 获取图片的旋转角度 * * @param path 图片绝对路径 * @return 图片的旋转角度 */ public static int getBitmapDegree(String path) { int degree = 0; try { // 从指定路径下读取图片,并获取其EXIF信息 ExifInterface exifInterface = new ExifInterface(path); // 获取图片的旋转信息 int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } /** * 将图片按照指定的角度进行旋转 * * @param bitmap 需要旋转的图片 * @param degree 指定的旋转角度 * @return 旋转后的图片 */ public static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) { // 根据旋转角度,生成旋转矩阵 Matrix matrix = new Matrix(); matrix.postRotate(degree); // 将原始图片按照旋转矩阵进行旋转,并得到新的图片 Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); if (bitmap != null & !bitmap.isRecycled()) { bitmap.recycle(); } return newBitmap; }
5、进制之间的转换
// 十六进制转二进制public String HToB(String a) {String b = Integer.toBinaryString(Integer.valueOf(toD(a, 16)));return b;}// 二进制转十六进制public String BToH(String a) {// 将二进制转为十进制再从十进制转为十六进制String b = Integer.toHexString(Integer.valueOf(toD(a, 2)));return b;}// 任意进制数转为十进制数public String toD(String a, int b) {//其中b为10int r = 0;for (int i = 0; i < a.length(); i++) {r = (int) (r + formatting(a.substring(i, i + 1))* Math.pow(b, a.length() - i - 1));}return String.valueOf(r);}// 将十六进制中的字母转为对应的数字public int formatting(String a) {int i = 0;for (int u = 0; u < 10; u++) {if (a.equals(String.valueOf(u))) {i = u;}}if (a.equals("a")) {i = 10;}if (a.equals("b")) {i = 11;}if (a.equals("c")) {i = 12;}if (a.equals("d")) {i = 13;}if (a.equals("e")) {i = 14;}if (a.equals("f")) {i = 15;}return i;}// 将十进制中的数字转为十六进制对应的字母public String formattingH(int a) {String i = String.valueOf(a);switch (a) {10: i = "a";break;11: i = "b";break;12: i = "c";break;13: i = "d";break;14: i = "e";break;15: i = "f";break;}return i;}
6、Android获取屏幕宽度 android获取屏幕的高度和宽度用到WindowManager这个类,两种方法: 1、WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight(); 2、WindowManager wm = this.getWindowManager(); int width = wm.getDefaultDisplay().getWidth(); int height = wm.getDefaultDisplay().getHeight();
更多相关文章
- Android 获取手机上图片和视频的缩略图 Android thumbnails .
- Android ListView滑动过程中图片显示重复错位闪烁问题解决
- flutter包名,应用名称,图标,启动图片修改
- android中常见的四种布局
- Android TextView加载html图片详解
- Android入门(2) 基本控件介绍、4种布局
- android 调用图库中选择的图片
- 【Android开发问题集】布局加载器异常的问题