百分比布局

导包  compile 'com.android.support:percent:23.3.0'

在res文件下创建文件     它会自动根据你安装的手机屏幕进行选择适配

 

文件的名字可以随便起

                         layout           普通的单面版的屏幕
layout-large     屏幕尺寸大于7英寸,同时系统版本在3.2之下
layout-sw600dp 
                         layout-land    横屏
layout(layout-port) 竖屏
layout-sw600dp-land
layout-sw600dp-port

 

 

2.导包写代码    compile 'com.android.support:percent:23.3.0'

这里包括了两种布局: PercentRelativeLayout、PercentFrameLayout

支持的属性有:
    layout_widthPercent、layout_heightPercent、
    layout_marginPercent、layout_marginLeftPercent、
    layout_marginTopPercent、layout_marginRightPercent、
    layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。

 

3.主布局

 

 
  1. <android.support.percent.PercentRelativeLayout

  2. xmlns:android="http://schemas.android.com/apk/res/android"

  3. xmlns:app="http://schemas.android.com/apk/res-auto"

  4. android:layout_width="match_parent"

  5. android:layout_height="match_parent"

  6. android:orientation="vertical">

  7. <TextView

  8. android:id="@+id/one_tv"

  9. android:layout_height="0dp"

  10. android:layout_width="0dp"

  11. android:text="w-40%,h-20%"

  12. android:gravity="center"

  13. android:layout_alignParentTop="true"

  14. app:layout_heightPercent="20%"

  15. app:layout_widthPercent="40%"

  16. android:background="#44ff0000"/>

  17. <TextView

  18. android:id="@+id/two_tv"

  19. android:layout_height="0dp"

  20. android:layout_width="0dp"

  21. android:gravity="center"

  22. android:text="w-30%,h-10%"

  23. android:background="#440000ff"

  24. app:layout_heightPercent="10%"

  25. app:layout_widthPercent="30%"

  26. app:layout_marginPercent="10%"

  27. android:layout_alignParentRight="true"/>

  28.  
  29.  
  30. </android.support.percent.PercentRelativeLayout>


 

 

 

 

 

4.当然在当使用图片时,无法设置宽高的比例比如我们的图片宽高是200*100的,我们在使用过程中我们设置宽高为20%、10%,这样会造成图片的比例失调。

为什么呢?因为20%参考的是屏幕的宽度,而10%参考的是屏幕的高度。很难使用百分比定义一个正方形的控件

比如,我现在界面的右下角有一个FloatingActionButton,我希望其宽度和高度都为屏幕宽度的10%,很难做到。

所以可以写下面的代码

 

 
  1. <com.zhy.android.percent.support.PercentLinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"

  2. xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical">

  6. <TextView

  7. android:layout_width="0dp"

  8. android:layout_height="0dp"

  9. android:gravity="center"

  10. android:text="width-30%,height-20%"

  11. app:layout_heightPercent="20%"

  12. app:layout_widthPercent="30%"

  13. android:background="@color/colorAccent"/>

  14. <TextView

  15. android:layout_width="0dp"

  16. android:layout_height="0dp"

  17. android:gravity="center"

  18. android:text="width-40%,height-20%"

  19. app:layout_heightPercent="20%w"

  20. app:layout_widthPercent="40%"

  21. android:background="@color/colorPrimary"/>

  22. </com.zhy.android.percent.support.PercentLinearLayout>


 

 

 

 

5.主函数获取宽高和版本号

 

 
  1. public class MainActivity extends AppCompatActivity {

  2.  
  3. private WindowManager wm;

  4.  
  5. @Override

  6. protected void onCreate(Bundle savedInstanceState) {

  7. super.onCreate(savedInstanceState);

  8. setContentView(R.layout.main1);

  9. }

  10.  
  11. // 获取屏幕的宽度和高度的方法

  12. public void getWidthAndHeight(){

  13. wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

  14. int height = wm.getDefaultDisplay().getHeight();

  15. int width = wm.getDefaultDisplay().getWidth();

  16. Log.i("tag","height==="+height+",width==="+width);

  17. }

  18.  
  19. // 获取屏幕宽度和高度的第二种方法

  20. public void getScreenWAndH(){

  21. wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

  22. DisplayMetrics outMetrics = new DisplayMetrics();

  23. wm.getDefaultDisplay().getMetrics(outMetrics);

  24.  
  25. int widthPixels = outMetrics.widthPixels;

  26. int heightPixels = outMetrics.heightPixels;

  27. Log.i("tag","height==="+heightPixels+",width==="+widthPixels);

  28. }

  29.  
  30. // 获取包名和版本号的方法

  31. public void getPackageInfo(){

  32. String packageName = this.getPackageName();

  33. Log.i("TAG","包名==="+packageName);

  34. // 获取应用包管理器

  35. PackageManager pm = this.getPackageManager();

  36.  
  37. try {

  38. PackageInfo info = pm.getPackageInfo(packageName, 0);

  39.  
  40. int versionCode = info.versionCode;

  41. String versionName = info.versionName;

  42. Log.i("TAG","code==="+versionCode+",name==="+versionName);

  43. } catch (PackageManager.NameNotFoundException e) {

  44. e.printStackTrace();

  45. }

  46. }

  47. }

  48. /**

  49. * 版本迭代的过程:

  50. * 打开app---》如果程序更新了---》会跳出对话框,提示更新。用户可以选择更新或者不更新。

  51. *

  52. * 程序员的逻辑:

  53. * 1.在首页面,开发时,服务器会自动提供一个控制版本更新的接口url。

  54. * 2.每一次用户打开app,都会自动连接这个网址,然后这个返回json数据,{ret:1,status:'连接成功了',versioncode:2,url:"http://......"}

  55. * 3.解析网址,拿出网址当中的版本号,和本地app的版本号进行对比,如果最新的版本和本地的不一致,就下载url对应的apk

  56. * 4.通常下载显示在通知栏,而且有进度条,下载成功之后,提示用户安装新版本。


 

 

 

 

6.你也可以添加一个图片加工代码

 

 
  1. 图片的加工处理代码:

  2. //1.得到用来设置图片尺寸的参数的对象

  3. BitmapFactory.Options options = new BitmapFactory.Options();

  4. //2.解码边缘

  5. options.inJustDecodeBounds = true;

  6. //3.对图片进行解码

  7. BitmapFactory.decodeByteArray(data, 0, data.length, options);

  8. //4.获取图片原来的宽度和高度

  9. int oldWidth = options.outWidth;

  10. int oldHeight = options.outHeight;

  11. //5.得到压缩的比例

  12. double scaleWidth = oldWidth/newWidth; //宽度要压缩的比例

  13. double scaleHeight = oldHeight/newHeight; //高度要压缩的比例。

  14. //6.取出宽高的压缩比例当中较大的值作为缩放比例

  15. int scale = (int) Math.round(scaleHeight>scaleWidth?scaleHeight:scaleWidth);

  16. //7.设置参数当中的缩放比例,必须要设置大于1的整数,数越大,缩放越小

  17. options.inSampleSize = scale;

  18. //8.缩放边缘

  19. options.inJustDecodeBounds = false;

  20.  
  21. //9.通过属性参数对象得到新的位图

  22. Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length,options);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值