先从一个效果说起吧

注意下几点:
  1. 圆角
  2. 按下的效果
  3. 同一个布局,两行效果和一行效果
  4. 最外还有 1px 的线条

可能本人才疏学浅,最终是在别人的帮助才完成这个效果的。

一共需要注意的几个点:

  1. 圆角用的是自定义控件
  2. 自定义控件可以控制最外层是否有线条
  3. 点击效果
  4. 单行变双行,双行变单行怎么做到
  5. 如何弹出界面在 window 上面,
正式开始
  1. 圆角的自定义控件

    1. 复制可用
      public class CornerLinearLayout extends LinearLayout {
          private final RectF roundRect = new RectF();
          private float rect_adius = getResources().getDimension(R.dimen.m2);
          private int borderWidth = 0;
          private final Paint maskPaint = new Paint();
          private final Paint zonePaint = new Paint();
          private Paint borderPaint = null;
      
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">CornerLinearLayout</span><span class="hljs-params">(@NonNull Context context)</span> </span>{
          <span class="hljs-keyword">this</span>(context, <span class="hljs-keyword">null</span>);
      }
      
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">CornerLinearLayout</span><span class="hljs-params">(@NonNull Context context, @Nullable AttributeSet attrs)</span> </span>{
          <span class="hljs-keyword">this</span>(context, attrs, <span class="hljs-number">0</span>);
      }
      
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">CornerLinearLayout</span><span class="hljs-params">(@NonNull Context context, @Nullable AttributeSet attrs, <span class="hljs-keyword">int</span> defStyleAttr)</span> </span>{
          <span class="hljs-keyword">super</span>(context, attrs, defStyleAttr);
          getAttrs(context, attrs);
          init();
      }
      
      <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">getAttrs</span><span class="hljs-params">(Context context, AttributeSet attrs)</span> </span>{
          TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CornerLinearLayout);
          rect_adius = ta.getDimension(R.styleable.CornerLinearLayout_cornerRadius, rect_adius);
          borderWidth = ta.getDimensionPixelOffset(R.styleable.CornerLinearLayout_border_width, -<span class="hljs-number">1</span>);
          <span class="hljs-keyword">if</span> (BuildConfig.DEBUG) {
              Log.d(<span class="hljs-string">"telenewbie::"</span>, getClass().getSimpleName() + <span class="hljs-string">",getAttrs"</span> + borderWidth);
          }
      
          ta.recycle();
      }
      
      <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>{
          maskPaint.setAntiAlias(<span class="hljs-keyword">true</span>);
          maskPaint.setXfermode(<span class="hljs-keyword">new</span> PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
          zonePaint.setAntiAlias(<span class="hljs-keyword">true</span>);
          zonePaint.setColor(Color.parseColor(<span class="hljs-string">"#000000"</span>));
          <span class="hljs-keyword">if</span> (borderWidth &gt; <span class="hljs-number">0</span>) {
              borderPaint = <span class="hljs-keyword">new</span> Paint();
              borderPaint.setStrokeWidth(borderWidth);
              borderPaint.setAntiAlias(<span class="hljs-keyword">true</span>);
              borderPaint.setStyle(Paint.Style.STROKE);
              borderPaint.setColor(Color.parseColor(<span class="hljs-string">"#19FFFFFF"</span>));
          }
      
          <span class="hljs-keyword">float</span> density = getResources().getDisplayMetrics().density;
          rect_adius = rect_adius * density;
      }
      
      <span class="hljs-meta">@Override</span>
      <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onLayout</span><span class="hljs-params">(<span class="hljs-keyword">boolean</span> changed, <span class="hljs-keyword">int</span> left, <span class="hljs-keyword">int</span> top, <span class="hljs-keyword">int</span> right, <span class="hljs-keyword">int</span> bottom)</span> </span>{
          <span class="hljs-keyword">super</span>.onLayout(changed, left, top, right, bottom);
          <span class="hljs-keyword">int</span> w = getWidth();
          <span class="hljs-keyword">int</span> h = getHeight();
          roundRect.set(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, w, h);
      }
      
      <span class="hljs-meta">@Override</span>
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">draw</span><span class="hljs-params">(Canvas canvas)</span> </span>{
          <span class="hljs-comment">// 创建图层A,绘制圆角矩阵</span>
          canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
          canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);
          <span class="hljs-comment">// 创建图层B,使用xfermode将图层的形状变成圆角矩阵</span>
          canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
          <span class="hljs-comment">// 清空图层的颜色</span>
          canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
          <span class="hljs-keyword">super</span>.draw(canvas);
          <span class="hljs-comment">// 将图层B绘制到canvas上</span>
          <span class="hljs-keyword">if</span> (borderPaint != <span class="hljs-keyword">null</span>) {
              canvas.drawRoundRect(roundRect, rect_adius, rect_adius, borderPaint);
          }
          canvas.restore();
      }
      
      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setCorner</span><span class="hljs-params">(<span class="hljs-keyword">float</span> adius)</span> </span>{
          rect_adius = adius;
          invalidate();
      }
      

      }

      复制代码
  2. 控制线条

    1. 在 attrs.xml 中加上这个来进行控制圆角大小以及边框大小
     <declare-styleable name="CornerLinearLayout">
             <attr name="cornerRadius" />
             <attr name="border_width" />
     </declare-styleable>
    复制代码
  3. 点击效果

    1. 一开始真的没有想到这个效果要怎么办,后来大神说:透明就可以了。于是就变成这样了。base_dialog_btn_bg.xml

      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:drawable="@color/white_10" android:state_pressed="true" />
          <item android:drawable="@color/transparent" />
      </selector>
      复制代码
    2. 之后只要对点击的范围设置背景色就好了 eg:

      <TextView
              android:id="@+id/tv_cancel"
              android:layout_width="@dimen/m112"
              android:layout_height="match_parent"
              android:background="@drawable/base_dialog_btn_bg"
              android:gravity="center"
              android:textSize="@dimen/base_tv_h5"
              android:text="取消" />
      复制代码
  4. 单行变双行

    1. 我用的方式是隐藏 + 改变组件大小

      <com.txznet.music.widget.CornerLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:id="@+id/rl_root"
          android:layout_width="match_parent"
          android:layout_height="@dimen/m112"
          android:layout_gravity="center_horizontal"
          android:layout_marginLeft="@dimen/m32"
          android:layout_marginRight="@dimen/m32"
          android:layout_marginTop="@dimen/m16"
          android:orientation="horizontal"
          android:background="@color/base_dialog_bg"
          app:border_width="@dimen/m1"
          app:cornerRadius="@dimen/m8">
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
              android:orientation="vertical">
              <LinearLayout
                  android:id="@+id/ll_first_range"
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1"
                  android:background="@drawable/base_dialog_btn_bg"
                  android:gravity="center_vertical"
                  android:orientation="horizontal">
      
          <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>
      
          <span class="hljs-tag">&lt;<span class="hljs-name">View</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"@style/Base_Divider_Horizontal"</span> /&gt;</span>
      
          <span class="hljs-tag">&lt;<span class="hljs-name">LinearLayout</span>
              <span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/ll_second_range"</span>
              <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
              <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"0dp"</span>
              <span class="hljs-attr">android:layout_weight</span>=<span class="hljs-string">"1"</span>
              <span class="hljs-attr">android:background</span>=<span class="hljs-string">"@drawable/base_dialog_btn_bg"</span>
              <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center_vertical"</span>
              <span class="hljs-attr">android:orientation</span>=<span class="hljs-string">"horizontal"</span>
              <span class="hljs-attr">android:visibility</span>=<span class="hljs-string">"visible"</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>
      
      <span class="hljs-tag">&lt;/<span class="hljs-name">LinearLayout</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">View</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"@style/Base_Divider_Vertical"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span>
          <span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/tv_cancel"</span>
          <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"@dimen/m112"</span>
          <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"match_parent"</span>
          <span class="hljs-attr">android:background</span>=<span class="hljs-string">"@drawable/base_dialog_btn_bg"</span>
          <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">"center"</span>
          <span class="hljs-attr">android:textSize</span>=<span class="hljs-string">"@dimen/base_tv_h5"</span>
          <span class="hljs-attr">android:text</span>=<span class="hljs-string">"取消"</span> /&gt;</span>
      

      </com.txznet.music.widget.CornerLinearLayout>

      复制代码
    2. 然后变成一行只需要将最外层的控件大小改为一行的大小,比方说我这里设计师给到的单行的高度是:72【800x480 的设备】

      private void changeLine(int style) {
              ViewGroup.LayoutParams layoutParams = mView.findViewById(R.id.rl_root).getLayoutParams();
              if (layoutParams == null) {
                  layoutParams = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
              }
              if (STYLE_DOUBLE == style) {
                  layoutParams.height = ((int) GlobalContext.get().getResources().getDimension(R.dimen.m112));
              } else if (STYLE_SINGLE == style) {
                  llSecondRange.setVisibility(GONE);
                  layoutParams.height = ((int) GlobalContext.get().getResources().getDimension(R.dimen.m72));
              }
              mView.findViewById(R.id.rl_root).setLayoutParams(layoutParams);
          }
      复制代码
  5. 添加到主 window 到屏幕上

    1. 本人 4.4 的设备,所以权限方面没有考虑,因为框架默认申请 5.0 所有的权限

      mWinManager = (WindowManager) GlobalContext.get().getSystemService(Context.WINDOW_SERVICE);
      mLayoutParam = new WindowManager.LayoutParams();
      mLayoutParam.width = WindowManager.LayoutParams.MATCH_PARENT;
      mLayoutParam.height = WindowManager.LayoutParams.WRAP_CONTENT;
      mLayoutParam.type = WindowManager.LayoutParams.TYPE_PHONE;
      mLayoutParam.flags = 40;
      mLayoutParam.format = PixelFormat.RGBA_8888;
      mLayoutParam.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
      mWinManager.addView(PushNotification.this, mLayoutParam);
      复制代码

简单适配方案

  1. 也许你注意到了代码里面或者 xml 里面设置长宽都是采用@dimen/m120这样的方式来进行的,因为这里采用的不是今日头条的适配方案,而是再早之前的等比例缩放的方式即按照基准(800x480)来进行换算不同屏幕的分辨率底下的值,比方说:在 800x480 上面的宽 1px 相当于 1024x600 上面的几个像素,高 1px 相当于 1024x600 上面的多少个像素?

    经过换算需要显示的大小为:h = 1px * 600/480 = 1.25px w = 1px * 1024/800 = 1.28px

    也许你会有疑问【可能经历过才知道,在车载环境里面各种奇葩的设备,防不胜防,有的长度很长,但高度很短,有的高度很高,但是长度很短】,那不是我一个正方形的图片会被拉成奇怪的长条状。正式因为考虑到这个【或者说遇到这个 bug】,所以有了最小比例之说,比方说:上面的比例 1.25 和 1.28 我们就用最小的分辨率来进行换算生成一套最小的分辨率来进行设置,比方说:1.25 和 1.28 我们就采用 1.25 的规则,于是就有了这样的一个文件:【当然这个文件可以通过 java 来进行自动生成,写好规则就好了】

    <?xml version="1.0" encoding="utf-8"?>
    <resources><dimen name="m1">1.25px</dimen>
    <dimen name="m2">2.5px</dimen>
    <dimen name="m3">3.75px</dimen>
    <dimen name="m4">5.0px</dimen>
    <dimen name="m5">6.25px</dimen>
    <dimen name="m6">7.5px</dimen>
    <dimen name="m7">8.75px</dimen>
    <dimen name="m8">10.0px</dimen>
    <dimen name="m9">11.25px</dimen>
    <dimen name="m10">12.5px</dimen>
    //m11 --- m469
    <dimen name="m470">587.5px</dimen>
    <dimen name="m471">588.75px</dimen>
    <dimen name="m472">590.0px</dimen>
    <dimen name="m473">591.25px</dimen>
    <dimen name="m474">592.5px</dimen>
    <dimen name="m475">593.75px</dimen>
    <dimen name="m476">595.0px</dimen>
    <dimen name="m477">596.25px</dimen>
    <dimen name="m478">597.5px</dimen>
    <dimen name="m479">598.75px</dimen>
    <dimen name="m480">600.0px</dimen>
    </resources>
    复制代码
  2. 这里附上生成规则的 java 代码【当然基准是可以修改的】

    public class GenerateValueFiles {
    
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> baseW;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> baseH;
    
    <span class="hljs-keyword">private</span> String dirStr = <span class="hljs-string">"./res"</span>;
    
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> String WTemplate = <span class="hljs-string">"&lt;dimen name=\"x{0}\"&gt;{1}px&lt;/dimen&gt;\n"</span>;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> String HTemplate = <span class="hljs-string">"&lt;dimen name=\"y{0}\"&gt;{1}px&lt;/dimen&gt;\n"</span>;
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> String SizeTemplate = <span class="hljs-string">"&lt;dimen name=\"m{0}\"&gt;{1}px&lt;/dimen&gt;\n"</span>;
    
    <span class="hljs-comment">/**
     * {0}-HEIGHT
     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> String VALUE_TEMPLATE = <span class="hljs-string">"values-{0}x{1}"</span>;
    
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> String SUPPORT_DIMESION = <span class="hljs-string">"800,480;960,540;1280,720;1920,1080;1280,400;1200,400;1280,720;2048,"</span>
    		+ <span class="hljs-string">"1536;1600,480;1280,480;854,480;480,272;800,432;1200,480;710,440;800,440;1920,480;980,400;1280,408;1280,352;"</span>
    		+ <span class="hljs-string">"1280,408;694,480;650,480;1184,384;1024,600;1024,720;1920,1200;1076,736;1000,700;480,320"</span>;
    
    <span class="hljs-keyword">private</span> String supportStr = SUPPORT_DIMESION;
    
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">GenerateValueFiles</span><span class="hljs-params">(<span class="hljs-keyword">int</span> baseX, <span class="hljs-keyword">int</span> baseY, String supportStr)</span> </span>{
    	<span class="hljs-keyword">this</span>.baseW = baseX;
    	<span class="hljs-keyword">this</span>.baseH = baseY;
    
    	<span class="hljs-keyword">if</span> (!<span class="hljs-keyword">this</span>.supportStr.contains(baseX + <span class="hljs-string">","</span> + baseY)) {
    		<span class="hljs-keyword">this</span>.supportStr += baseX + <span class="hljs-string">","</span> + baseY + <span class="hljs-string">";"</span>;
    	}
    
    	<span class="hljs-keyword">this</span>.supportStr += validateInput(supportStr);
    
    	System.out.println(supportStr);
    
    	File dir = <span class="hljs-keyword">new</span> File(dirStr);
    	<span class="hljs-keyword">if</span> (!dir.exists()) {
    		dir.mkdir();
    
    	}
    	System.out.println(dir.getAbsoluteFile());
    
    }
    
    <span class="hljs-comment">/**
     * <span class="hljs-doctag">@param</span> supportStr
     *            w,h_...w,h;
     * <span class="hljs-doctag">@return</span>
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">private</span> String <span class="hljs-title">validateInput</span><span class="hljs-params">(String supportStr)</span> </span>{
    	StringBuffer sb = <span class="hljs-keyword">new</span> StringBuffer();
    	String[] vals = supportStr.split(<span class="hljs-string">"_"</span>);
    	<span class="hljs-keyword">int</span> w = -<span class="hljs-number">1</span>;
    	<span class="hljs-keyword">int</span> h = -<span class="hljs-number">1</span>;
    	String[] wh;
    	<span class="hljs-keyword">for</span> (String val : vals) {
    		<span class="hljs-keyword">try</span> {
    			<span class="hljs-keyword">if</span> (val == <span class="hljs-keyword">null</span> || val.trim().length() == <span class="hljs-number">0</span>)
    				<span class="hljs-keyword">continue</span>;
    
    			wh = val.split(<span class="hljs-string">","</span>);
    			w = Integer.parseInt(wh[<span class="hljs-number">0</span>]);
    			h = Integer.parseInt(wh[<span class="hljs-number">1</span>]);
    		} <span class="hljs-keyword">catch</span> (Exception e) {
    			System.out.println(<span class="hljs-string">"skip invalidate params : w,h = "</span> + val);
    			<span class="hljs-keyword">continue</span>;
    		}
    		sb.append(w + <span class="hljs-string">","</span> + h + <span class="hljs-string">";"</span>);
    	}
    
    	<span class="hljs-keyword">return</span> sb.toString();
    }
    
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">generate</span><span class="hljs-params">()</span> </span>{
    	String[] vals = supportStr.split(<span class="hljs-string">";"</span>);
    	<span class="hljs-keyword">for</span> (String val : vals) {
    		String[] wh = val.split(<span class="hljs-string">","</span>);
    		generateXmlFile(Integer.parseInt(wh[<span class="hljs-number">0</span>]), Integer.parseInt(wh[<span class="hljs-number">1</span>]));
    	}
    
    }
    
    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">generateXmlFile</span><span class="hljs-params">(<span class="hljs-keyword">int</span> w, <span class="hljs-keyword">int</span> h)</span> </span>{
    
    	<span class="hljs-comment">// </span>
    	<span class="hljs-keyword">float</span> cellw = w * <span class="hljs-number">1.0f</span> / baseW;
    	StringBuffer sbForWidth = <span class="hljs-keyword">new</span> StringBuffer();
    	sbForWidth.append(<span class="hljs-string">"&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n"</span>);
    	sbForWidth.append(<span class="hljs-string">"&lt;resources&gt;"</span>);
    	System.out.println(<span class="hljs-string">"width : "</span> + w + <span class="hljs-string">","</span> + baseW + <span class="hljs-string">","</span> + cellw );
    	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; baseW; i++) {
    		sbForWidth.append(WTemplate.replace(<span class="hljs-string">"{0}"</span>, i + <span class="hljs-string">""</span>).replace(<span class="hljs-string">"{1}"</span>, change(cellw * i) + <span class="hljs-string">""</span>));
    	}
    	sbForWidth.append(WTemplate.replace(<span class="hljs-string">"{0}"</span>, baseW + <span class="hljs-string">""</span>).replace(<span class="hljs-string">"{1}"</span>, w + <span class="hljs-string">""</span>));
    	sbForWidth.append(<span class="hljs-string">"&lt;/resources&gt;"</span>);
    	<span class="hljs-comment">// </span>
    	<span class="hljs-keyword">float</span> cellh = h * <span class="hljs-number">1.0f</span> / baseH;
    	StringBuffer sbForHeight = <span class="hljs-keyword">new</span> StringBuffer();
    	sbForHeight.append(<span class="hljs-string">"&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n"</span>);
    	sbForHeight.append(<span class="hljs-string">"&lt;resources&gt;"</span>);
    	System.out.println(<span class="hljs-string">"height : "</span> + h + <span class="hljs-string">","</span> + baseH + <span class="hljs-string">","</span> + cellh);
    	<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; baseH; i++) {
    		sbForHeight.append(HTemplate.replace(<span class="hljs-string">"{0}"</span>, i + <span class="hljs-string">""</span>).replace(<span class="hljs-string">"{1}"</span>, change(cellh * i) + <span class="hljs-string">""</span>));
    	}
    	sbForHeight.append(HTemplate.replace(<span class="hljs-string">"{0}"</span>, baseH + <span class="hljs-string">""</span>).replace(<span class="hljs-string">"{1}"</span>, h + <span class="hljs-string">""</span>));
    	sbForHeight.append(<span class="hljs-string">"&lt;/resources&gt;"</span>);
    
    	<span class="hljs-comment">// </span>
    	<span class="hljs-keyword">float</span> minSize = (cellw &lt; cellh) ? cellw : cellh;
    	StringBuffer sbMinSize = <span class="hljs-keyword">new</span> StringBuffer();
    	sbMinSize.append(<span class="hljs-string">"&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n"</span>);
    	sbMinSize.append(<span class="hljs-string">"&lt;resources&gt;"</span>);
    	System.out.println(<span class="hljs-string">"height : "</span> + minSize + <span class="hljs-string">","</span> + cellw + <span class="hljs-string">","</span> + cellh);
    	
    	<span class="hljs-keyword">if</span>(h&lt;baseH){
    		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; baseH; i++) {
    			sbMinSize.append(SizeTemplate.replace(<span class="hljs-string">"{0}"</span>, i + <span class="hljs-string">""</span>).replace(<span class="hljs-string">"{1}"</span>, change(<span class="hljs-number">1.0f</span> * i) + <span class="hljs-string">""</span>));
    		}
    	}<span class="hljs-keyword">else</span>{
    		<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">1</span>; i &lt; baseH; i++) {
    			sbMinSize.append(SizeTemplate.replace(<span class="hljs-string">"{0}"</span>, i + <span class="hljs-string">""</span>).replace(<span class="hljs-string">"{1}"</span>, change(minSize * i) + <span class="hljs-string">""</span>));
    		}
    	}
    	sbMinSize.append(SizeTemplate.replace(<span class="hljs-string">"{0}"</span>, ((cellw &lt; cellh) ?baseW:baseH) + <span class="hljs-string">""</span>).replace(<span class="hljs-string">"{1}"</span>, ((cellw &lt; cellh) ?w:h) + <span class="hljs-string">""</span>));
    	sbMinSize.append(<span class="hljs-string">"&lt;/resources&gt;"</span>);
    	
    	
    
    	File fileDir = <span class="hljs-keyword">new</span> File(dirStr + File.separator + VALUE_TEMPLATE.replace(<span class="hljs-string">"{0}"</span>, w + <span class="hljs-string">""</span>)<span class="hljs-comment">//</span>
    			.replace(<span class="hljs-string">"{1}"</span>, h + <span class="hljs-string">""</span>));
    	fileDir.mkdir();
    
    	File layxFile = <span class="hljs-keyword">new</span> File(fileDir.getAbsolutePath(), <span class="hljs-string">"lay_x.xml"</span>);
    	File layyFile = <span class="hljs-keyword">new</span> File(fileDir.getAbsolutePath(), <span class="hljs-string">"lay_y.xml"</span>);
    	File laysizeFile = <span class="hljs-keyword">new</span> File(fileDir.getAbsolutePath(), <span class="hljs-string">"lay_size.xml"</span>);
    	<span class="hljs-keyword">try</span> {
    		PrintWriter pw = <span class="hljs-keyword">new</span> PrintWriter(<span class="hljs-keyword">new</span> FileOutputStream(layxFile));
    		pw.print(sbForWidth.toString());
    		pw.close();
    		pw = <span class="hljs-keyword">new</span> PrintWriter(<span class="hljs-keyword">new</span> FileOutputStream(layyFile));
    		pw.print(sbForHeight.toString());
    		pw.close();
    		pw = <span class="hljs-keyword">new</span> PrintWriter(<span class="hljs-keyword">new</span> FileOutputStream(laysizeFile));
    		pw.print(sbMinSize.toString());
    		pw.close();
    	} <span class="hljs-keyword">catch</span> (FileNotFoundException e) {
    		e.printStackTrace();
    	}
    }
    
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">float</span> <span class="hljs-title">change</span><span class="hljs-params">(<span class="hljs-keyword">float</span> a)</span> </span>{
    	<span class="hljs-keyword">int</span> temp = (<span class="hljs-keyword">int</span>) (a * <span class="hljs-number">100</span>);
    	<span class="hljs-keyword">return</span> temp / <span class="hljs-number">100f</span>;
    }
    
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
    	<span class="hljs-keyword">int</span> baseW = <span class="hljs-number">800</span>;
    	<span class="hljs-keyword">int</span> baseH = <span class="hljs-number">480</span>;
    	String addition = <span class="hljs-string">""</span>;
    	<span class="hljs-keyword">try</span> {
    		<span class="hljs-keyword">if</span> (args.length &gt;= <span class="hljs-number">3</span>) {
    			baseW = Integer.parseInt(args[<span class="hljs-number">0</span>]);
    			baseH = Integer.parseInt(args[<span class="hljs-number">1</span>]);
    			addition = args[<span class="hljs-number">2</span>];
    		} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (args.length &gt;= <span class="hljs-number">2</span>) {
    			baseW = Integer.parseInt(args[<span class="hljs-number">0</span>]);
    			baseH = Integer.parseInt(args[<span class="hljs-number">1</span>]);
    		} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (args.length &gt;= <span class="hljs-number">1</span>) {
    			addition = args[<span class="hljs-number">0</span>];
    		}
    	} <span class="hljs-keyword">catch</span> (NumberFormatException e) {
    
    		System.err.println(<span class="hljs-string">"right input params : java -jar xxx.jar width height w,h_w,h_..._w,h;"</span>);
    		e.printStackTrace();
    		System.exit(-<span class="hljs-number">1</span>);
    	}
    
    	<span class="hljs-keyword">new</span> GenerateValueFiles(baseW, baseH, addition).generate();
    }
    

    }

    复制代码
  3. 这里还要讲下,动态计算 gridview 需要填充的列数, 在可填充的最大列数上呈现不足最大列数个数的时候保持位置固定而非居中,在呈现最大列数的时候能够保持左右间距相等

    mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    int measuredWidth = mRecyclerView.getMeasuredWidth();// 控件总共拥有的空间大小
                    int startSize = getResources().getDimensionPixelOffset(R.dimen.m160);// 一个 Item 的宽度
                    int count = 1;// 最大的列数
                    int spanSize = 0;// 剩余的宽度
                    for (int i = 0; i < 10; i++) {
                        startSize += getResources().getDimensionPixelOffset(R.dimen.m24);//item 与 item 之间的间距
                        startSize += getResources().getDimensionPixelOffset(R.dimen.m160);
                        if (startSize > measuredWidth) {
                            break;
                        }
                        spanSize = measuredWidth - startSize;
                        count++;
                    }
                    if (BuildConfig.DEBUG) {
                        Log.d("telenewbie::", getClass().getSimpleName() + ",onGlobalLayout:" + count + "," + startSize + "," + measuredWidth + "," + spanSize);
                    }
                    mRecyclerView.setPadding(spanSize / 2, 0, spanSize / 2, 0);
                    GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), count);
                    gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
                    mRecyclerView.setLayoutManager(gridLayoutManager);
                }
            });
    复制代码

今天就到这里吧

  • Android

    开放手机联盟(一个由 30 多家科技公司和手机公司组成的团体)已开发出 Android,Android 是第一个完整、开放、免费的手机平台。

    293 引用
感谢    赞同    分享    收藏    关注    反对    举报    ...