15.瀑布流、測量

排行界面

TopProtocol?:json數據就是寫字符串,所以不需要寫bean對象
  1. public class TopProtocol extends BaseProtocol<List<String>> {
  2. @Override
  3. public List<String> paserJson(String json) {
  4. List<String> datas=new ArrayList<String>();
  5. try {
  6. JSONArray array=new JSONArray(json);
  7. for(int i=0;i<array.length();i++){
  8. String str=array.getString(i);
  9. datas.add(str);
  10. }
  11. return datas;
  12. } catch (JSONException e) {
  13. e.printStackTrace();
  14. return null;
  15. }
  16. }
  17. @Override
  18. public String getKey() {
  19. return "hot";
  20. }
  21. }
DrawableUtils?:用代碼創建狀態選擇器 和圓角
  1. public class DrawableUtils {
  2. public static GradientDrawable createShape(int color){
  3. GradientDrawable drawable=new GradientDrawable();//相當于shape
  4. drawable.setCornerRadius(UiUtils.dip2px(5));//設置4個角的弧度
  5. drawable.setColor(color);// 設置顏色
  6. return drawable;
  7. }
  8. public static StateListDrawable createSelectorDrawable(Drawable pressedDrawable,Drawable normalDrawable){
  9. // <selector xmlns:android="http://schemas.android.com/apk/res/android" android:enterFadeDuration="200">
  10. // <item android:state_pressed="true" android:drawable="@drawable/detail_btn_pressed"></item>
  11. // <item android:drawable="@drawable/detail_btn_normal"></item>
  12. // </selector>
  13. StateListDrawable stateListDrawable=new StateListDrawable();
  14. stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);// 按下顯示的圖片
  15. stateListDrawable.addState(new int[]{}, normalDrawable);// 抬起顯示的圖片
  16. return stateListDrawable;
  17. }
  18. }
TopFragment?:使用代碼創建、隨機色
MATCH_PARENT?、FILL_PARENT是-1且他倆個沒有任何區別,WRAP_CONTENT是-2?
  1. public class TopFragment extends BaseFragment {
  2. private List<String> datas;
  3. @Override
  4. public View createSuccessView() {
  5. ScrollView scrollView=new ScrollView(UiUtils.getContext());
  6. scrollView.setBackgroundResource(R.drawable.grid_item_bg_normal);
  7. LinearLayout layout=new LinearLayout(UiUtils.getContext());
  8. int padding=UiUtils.dip2px(13);
  9. layout.setPadding(padding, padding, padding, padding);
  10. layout.setOrientation(LinearLayout.VERTICAL);// 設置線性布局的方向
  11. int backColor = 0xffcecece;
  12. Drawable pressedDrawable=DrawableUtils.createShape(backColor);// 按下顯示的圖片
  13. for(int i=0;i<datas.size();i++){
  14. TextView textView=new TextView(UiUtils.getContext());
  15. final String str=datas.get(i);
  16. textView.setText(str);
  17. Random random=new Random(); //創建隨機
  18. int red = random.nextInt(200)+22;
  19. int green = random.nextInt(200)+22;
  20. int blue = random.nextInt(200)+22;//有可能都是0或255成白色或者黑色了
  21. int color=Color.rgb(red, green, blue);//范圍 0-255
  22. GradientDrawable createShape = DrawableUtils.createShape(color); // 默認顯示的圖片
  23. StateListDrawable createSelectorDrawable = DrawableUtils.createSelectorDrawable(pressedDrawable, createShape);// 創建狀態選擇器
  24. textView.setBackgroundDrawable(createSelectorDrawable);
  25. textView.setTextColor(Color.WHITE);
  26. //textView.setTextSize(UiUtils.dip2px(14));
  27. int textPaddingV = UiUtils.dip2px(4);
  28. int textPaddingH = UiUtils.dip2px(7);
  29. textView.setPadding(textPaddingH, textPaddingV, textPaddingH, textPaddingV); //設置padding
  30. textView.setClickable(true);//設置textView可以被點擊
  31. textView.setOnClickListener(new OnClickListener() { // 設置點擊事件
  32. @Override
  33. public void onClick(View v) {
  34. Toast.makeText(UiUtils.getContext(), str, 0).show();
  35. }
  36. });
  37. layout.addView(textView,new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, -2));// -2 包裹內容
  38. }
  39. scrollView.addView(layout);
  40. return scrollView;
  41. }
  42. @Override
  43. protected LoadResult load() {
  44. TopProtocol protocol=new TopProtocol();
  45. datas = protocol.load(0);
  46. return checkData(datas);
  47. }
  48. }
到目前為止實現的效果是這樣的,將LinearLayout使用一個自定義控件


Flowlayout?
原理

827512-20151120194307608-686612542.png
827512-20151120194308327-246111244.png
  1. public class Flowlayout extends ViewGroup {
  2. private int horizontolSpacing=UiUtils.dip2px(13);
  3. private int verticalSpacing=UiUtils.dip2px(13);
  4. public Flowlayout(Context context) {
  5. super(context);
  6. }
  7. public Flowlayout(Context context, AttributeSet attrs, int defStyle) {
  8. super(context, attrs, defStyle);
  9. }
  10. private Line currentline;// 當前的行
  11. private int useWidth=0;// 當前行使用的寬度
  12. private List<Line> mLines=new ArrayList<Flowlayout.Line>();
  13. private int width;
  14. public Flowlayout(Context context, AttributeSet attrs) {
  15. super(context, attrs);
  16. }
  17. // 測量 當前控件Flowlayout
  18. // 父類是有義務測量每個孩子的
  19. @Override
  20. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  21. // TODO Auto-generated method stub
  22. // MeasureSpec.EXACTLY;
  23. // MeasureSpec.AT_MOST;
  24. // MeasureSpec.UNSPECIFIED;
  25. mLines.clear();
  26. currentline=null;
  27. useWidth=0;
  28. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  29. int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 獲取當前父容器(Flowlayout)的模式
  30. width = MeasureSpec.getSize(widthMeasureSpec)-getPaddingLeft()-getPaddingRight();
  31. int height = MeasureSpec.getSize(heightMeasureSpec)-getPaddingBottom()-getPaddingTop(); // 獲取到寬和高
  32. int childeWidthMode;
  33. int childeHeightMode;
  34. // 為了測量每個孩子 需要指定每個孩子測量規則
  35. childeWidthMode=(widthMode==MeasureSpec.EXACTLY)?MeasureSpec.AT_MOST:widthMode;
  36. childeHeightMode=heightMode==MeasureSpec.EXACTLY?MeasureSpec.AT_MOST:heightMode;
  37. int childWidthMeasureSpec=MeasureSpec.makeMeasureSpec(childeWidthMode, width);
  38. int childHeightMeasureSpec=MeasureSpec.makeMeasureSpec(childeHeightMode, height);
  39. currentline=new Line();// 創建了第一行
  40. for(int i=0;i<getChildCount();i++) {
  41. View child=getChildAt(i);
  42. System.out.println("孩子的數量:"+getChildCount());
  43. // 測量每個孩子
  44. child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
  45. int measuredWidth = child.getMeasuredWidth();
  46. useWidth+=measuredWidth;// 讓當前行加上使用的長度
  47. if(useWidth<=width){
  48. currentline.addChild(child);//這時候證明當前的孩子是可以放進當前的行里,放進去
  49. useWidth+=horizontolSpacing;
  50. if(useWidth>width){
  51. //換行
  52. newLine();
  53. }
  54. }else{
  55. //換行
  56. if(currentline.getChildCount()<1){
  57. currentline.addChild(child); // 保證當前行里面最少有一個孩子
  58. }
  59. newLine();
  60. }
  61. }
  62. if(!mLines.contains(currentline)){
  63. mLines.add(currentline);// 添加最后一行
  64. }
  65. int totalheight=0;
  66. for(Line line:mLines){
  67. totalheight+=line.getHeight();
  68. }
  69. totalheight+=verticalSpacing*(mLines.size()-1)+getPaddingTop()+getPaddingBottom();
  70. System.out.println(totalheight);
  71. setMeasuredDimension(width+getPaddingLeft()+getPaddingRight(),resolveSize(totalheight, heightMeasureSpec));
  72. }
  73. private void newLine() {
  74. mLines.add(currentline);// 記錄之前的行
  75. currentline=new Line(); // 創建新的一行
  76. useWidth=0;
  77. }
  78. private class Line{
  79. int height=0; //當前行的高度
  80. int lineWidth=0;
  81. private List<View> children=new ArrayList<View>();
  82. /**
  83. * 添加一個孩子
  84. * @param child
  85. */
  86. public void addChild(View child) {
  87. children.add(child);
  88. if(child.getMeasuredHeight()>height){
  89. height=child.getMeasuredHeight();
  90. }
  91. lineWidth+=child.getMeasuredWidth();
  92. }
  93. public int getHeight() {
  94. return height;
  95. }
  96. /**
  97. * 返回孩子的數量
  98. * @return
  99. */
  100. public int getChildCount() {
  101. return children.size();
  102. }
  103. public void layout(int l, int t) {
  104. lineWidth+=horizontolSpacing*(children.size()-1);
  105. int surplusChild=0;
  106. int surplus=width-lineWidth;
  107. if(surplus>0){
  108. surplusChild=surplus/children.size();
  109. }
  110. for(int i=0;i<children.size();i++){
  111. View child=children.get(i);
  112. // getMeasuredWidth() 控件實際的大小
  113. // getWidth() 控件顯示的大小
  114. child.layout(l, t, l+child.getMeasuredWidth()+surplusChild, t+child.getMeasuredHeight());
  115. l+=child.getMeasuredWidth()+surplusChild;
  116. l+=horizontolSpacing;
  117. }
  118. }
  119. }
  120. // 分配每個孩子的位置
  121. @Override
  122. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  123. l+=getPaddingLeft();
  124. t+=getPaddingTop();
  125. for(int i=0;i<mLines.size();i++){
  126. Line line=mLines.get(i);
  127. line.layout(l,t); //交給每一行去分配
  128. t+=line.getHeight()+verticalSpacing;
  129. }
  130. }
  131. }
如果不要平均分配那些步驟,實現的效果是這樣的
827512-20151120194312874-1799108277.png



自定義一個圓形的進度條
  1. public class ProgressView extends View {
  2. public ProgressView(Context context) {
  3. super(context);
  4. }
  5. public ProgressView(Context context, AttributeSet attrs, int defStyle) {
  6. super(context, attrs, defStyle);
  7. }
  8. public ProgressView(Context context, AttributeSet attrs) {
  9. super(context, attrs);
  10. }
  11. // 繪制控件
  12. @Override
  13. protected void onDraw(Canvas canvas) {
  14. super.onDraw(canvas);
  15. //canvas.drawBitmap(bitmap, left, top, paint);
  16. /*oval 圓的模型 矩形
  17. * startAngle 開始的角度
  18. * sweepAngle 范圍的角度
  19. * useCenter 是否填充中間部分
  20. * paint 畫筆
  21. */
  22. //canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
  23. }
  24. }



來自為知筆記(Wiz)


轉載于:https://www.cnblogs.com/sixrain/p/4982185.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/256390.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256390.shtml
英文地址,請注明出處:http://en.pswp.cn/news/256390.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linear-gradient線性漸變

background:linear-gradient(180deg, sliver 20%, skyblue 80%, gray 100%);180deg 是線性漸變的角度,水平方向;如果是90deg,則是垂直方向. silver 20% 是最上面的顏色和該顏色所在的位置,可以為負值,,如 linear-gradient(180deg, silver -7%, pink 80%, skyblue 127%);的效果是…

numpy——stack

np.stack(array,axis,outNone)&#xff0c;函數原型。 其中最重要是的這個axis怎么理解的。 舉例說明&#xff1a;arrays [np.random.randn(3, 4) for _ in range(10)] 會生成一個 10 *( 3 * 4 )的矩陣列表。十個矩陣&#xff0c;每個矩陣是(3 * 4)大小。 首先說明一下axis的映…

C# —— 簡單工廠設計模式詳述

一、基本概念 眾所周知&#xff0c;C#是一種面向對象的語言&#xff0c;而其中封裝&#xff0c;繼承&#xff0c;多態是面向對象的三大重要特征&#xff0c;簡單工廠的設計模式則可以完全體現這些特征。要徹底理解這個模式&#xff0c;必須要先將封裝&#xff08;訪問修飾符的…

【計算機視覺】計算機視覺、模式識別、機器學習常用牛人主頁鏈接

計算機視覺、模式識別、機器學習常用牛人主頁鏈接 牛人主頁&#xff08;主頁有很多論文代碼&#xff09; Serge Belongie at UC San DiegoAntonio Torralba at MITAlexei Ffros at CMUCe Liu at Microsoft Research New EnglandVittorio Ferrari at Univ.of EdinburghKristen G…

C# 中的 ConfigurationManager類引用方法

c#添加了Configuration;后&#xff0c;竟然找不到 ConfigurationManager 這個類&#xff0c;后來才發現&#xff1a;雖然引用了using System.Configuration;這個包&#xff0c;但是還是不行的。 后來終于找到一個解決方法&#xff0c;就是在解決方案資源管理器里找到類文件選擇…

機器學習——支持向量機SVM之python實現簡單實例一(含數據預處理、交叉驗證、參數優化等)

目錄 一、SVM理論 二、numpy的相關函數介紹 三、python實現之準備 1、數據集的下載

工業相機常用類型詳述

一、工業相機定義 工業相機是應用于工業領域、安防和交通等對相機要求較高領域的攝像機&#xff0c;功能就是將光信號轉變成有序的電信號&#xff0c;此信號經過模數轉換為數字信號&#xff0c;然后傳遞給圖像處理器。與一般的家用相機相比&#xff0c;其具有更高的穩定性能&a…

機器學習——SVM之python實現數據樣本標準化和歸一化

目錄 一、標準化和歸一化的目的 1、標準化 2、歸一化 二、標準化和歸一化常用的理論公式 1、歸一化 2、標準化 三、python實現SVM樣本數據標準化和歸一化 1、標準化 2、歸一化 本文源代碼&#xff1a;《機器學習——支持向量機SVM之python實現簡單實例一》 一、標準化…

[黑群暉經典教程] 一步一步建立自己的黑群暉

【申明&#xff1a;本文并非本人所作&#xff0c;為內部網絡中一位大神所寫&#xff0c;個人覺得寫得很好&#xff0c;遂原文搬了過來&#xff0c;如有侵犯原作者的權利&#xff0c;請及時與我聯系】 PS:有好幾個兄弟覺得我擅自轉發&#xff0c;不是很妥。解釋一下&#xff1a;…

Java為什么能跨平臺運行

因為java程序編譯之后的代碼不是能被硬件系統直接運行的代碼&#xff0c;而是一種“中間碼”--字節碼。不同的硬件平臺上裝有不同的java虛擬機&#xff08;JVM&#xff09;&#xff0c;由JVM來把字節碼再翻譯成所對應的硬件平臺能夠執行的代碼&#xff0c;因此java可以跨平臺運…

C++和Opencv4.5 實現全景圖像拼接

前言 最近剛下了最新版的opencv4.5&#xff0c;急不可待的試下操作&#xff0c;就用了opencv自帶的Stitcher類拼接下圖像&#xff0c;結果傻眼了&#xff0c;程序顯示Stitcher沒有createDefault成員&#xff0c;看了好久&#xff0c;終于找到了解決方法。 Stitcher原理 Stit…

機器學習——python實現SVM模型w,b的查看

基于源代碼&#xff1a;《機器學習——支持向量機SVM之python實現簡單實例一》進行講解 1、線性模型 這里以二特征三類&#xff0c;一對多策略為案例 kernel “linear”&#xff1a;線性核&#xff0c;參數有w&#xff0c;b 線性模型的決策邊界是&#xff1a;w0iTx0i w1i…

Codeforces-712C-Memory and De-Evolution

轉載于:https://www.cnblogs.com/GrowingJlx/p/6642764.html

移動端輸入框彈出鍵盤控制

在移動端&#xff0c;我們公司通過輸入框主要收集用戶的姓名和電話&#xff0c;以下是對輸入框獲取焦點時&#xff0c;控制彈出鍵盤的樣式來增強用戶體驗。 輸入姓名 我們的用戶都是中國人&#xff0c;輸入用戶名為中文&#xff0c;所以彈出鍵盤是輸入中文狀態即可&#xff0c;…

Opencv4.5-C++ 攝像頭畫面鏡像顯示及文件保存

前言 想試下新買電腦的攝像頭好用不&#xff0c;就寫了個攝像頭調用程序&#xff0c;實現了鏡像和圖片截取保存。 代碼 #include <iostream> #include <opencv2/stitching.hpp> #include <opencv2\opencv.hpp> #include <opencv2/highgui/highgui.h…

機器學習之支持向量機SVM之python實現ROC曲線繪制(二分類和多分類)

目錄 一、ROC曲線 二、TP、FP、TN、FN 三、 python繪制ROC曲線(二分類) 1、思路 2、關鍵代碼

easyui datagrid 列拖動

實現代碼-code <script type"text/javascript"> $.extend($.fn.datagrid.methods, { columnMoving: function(jq) { return jq.each(function() { var target this; var cells $(this).datagrid(getPanel).find(div.datagrid-header td[field]); cells.dragg…

window linux IPC ftok BY_HANDLE_FILE_INFORMATION

看這題目就很亂&#xff0c;心情當然也是不怎么美好了。前一段時間做了一個項目&#xff0c;AIX(Unix的一種&#xff09;中的一個系統向WINDOWS移植&#xff0c;開發環境由IBM的C/C(叫什么忘記了&#xff0c;好像是xlC)變為VC。 這是算過來&#xff0c;但是最近進程通信的信號量…

相機標定(一) —— 深入理解齊次坐標及其作用

一、什么是齊次坐標和齊次坐標系 齊次坐標 齊次坐標是一個相機標定問題的關鍵理論之一&#xff0c;所以就此問題分析一下。 單從定義上來講&#xff0c;齊次坐標&#xff08;投影坐標&#xff09;就是用N1維來代表N維坐標&#xff08;點和向量&#xff09;&#xff0c;也可說…

機器學習——圖解SVM中gamma和c參數的作用

參數c和gamma的作用 我們通過下圖詳解參數c的作用&#xff0c;首先我們以一個簡單的線性分類器為例&#xff0c;上一個博客中我們知道影響分類器的主要因素是支持向量&#xff0c;即虛線上的樣本&#xff0c;如下圖可知&#xff1a; 但當正負樣本的分布在如下情況時&#xff0…