zoukankan      html  css  js  c++  java
  • android_demo 之生成颜色

    老师说循环出颜色数字  然后显示出来  

    他说的什么一脸懵逼(=@__@=)   代码还在手上也还是懵逼 (づ。◕‿‿◕。)づ

    不管了   留个脚印在这 以后想起来   至少也知道

    直接上代码吧    说再多我也不懂啊(¬_¬)

    /DynamicTable/res/layout/activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="一键自动生成" />
        </LinearLayout>
    
        <TableLayout
            android:id="@+id/table1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </TableLayout>
    
    </LinearLayout>
    activity_main.xml

    /DynamicTable/src/com/example/dynamictable/MainActivity.java

    package com.example.dynamictable;
    
    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
        private final int MP = ViewGroup.LayoutParams.MATCH_PARENT;
        private Button bt1;
        private TableLayout tableLayout;
        private int c;
    
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 获取控件Button
            bt1 = (Button) findViewById(R.id.button1);
    
            // 给button按钮绑定单击事件
            bt1.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
                    // 获取控件tableLayout
                    tableLayout = (TableLayout) findViewById(R.id.table1);
                    // 清除表格所有行
                    tableLayout.removeAllViews();
                    // 全部列自动填充空白处
                    tableLayout.setStretchAllColumns(true);
                    // 生成X行,Y列的表格
    
                    int theLength = 64;
                    for (int i = 0; i < theLength; i++) {
                        TableRow tableRow = new TableRow(MainActivity.this);
                        for (int j = 0; j < theLength; j++) {
                            int result = i * theLength + (j + 1) - 1;// 0~4095
                            int red = result / 256;
                            int green = (result - red * 256) / 16;
                            int blue = result - red * 256 - green * 16;
    
                            // tv用于显示
                            TextView tv = new TextView(MainActivity.this);
    
                            // tv.setText("-");
                            int rgb = Color.rgb(red * 16, green * 16, blue * 16);
                            tv.setBackgroundColor(rgb);
    
                            // Color.rgb(red*16-1, green*16-1, blue*16-1)
                            // tv.setBackgroundResource(35434);
                            tableRow.addView(tv);
                            // System.out.println("i="+i+"; j="+j);
                            System.out.println(rgb + "=" + c++);
                        }
                        // 新建的TableRow添加到TableLayout
                        tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC, 1));
                    }
                }
            });
        }
    }
    MainActivity.java

    -_-|||

    这就是代码了  (o_ _)ノ  

    注意运行的时候会比较慢,当你一直看见那个页面不动,不要激动,请耐心等待,好事多磨。你懂的。

    由于考虑到电脑的因素我们就不循环全部颜色出来,只取其中的一部分。看见上面的颜色表,只是一部分而已,

    这是16的   就是  那个  int theLength = 64; 

    展示展示效果

     

    那什么  255什么的还是不敢试  

    测试了一半就强制退出了  囧rz=З  怕啊┑( ̄Д  ̄)┍   怕手机hold 不住啊。

     那什么 我还是瞎测试了一下 32的 就是   int theLength = 128; 的

    等了好久才出了╮(╯_╰)╭

    反正感觉它(ーー゛)好(@ ̄ー ̄@)丑

    搜图片有这样的

    感觉都要比那个循环出来的好看o( ̄ヘ ̄o#)

    这手机截屏怎么这么大啊   崩溃(# ̄~ ̄#)

  • 相关阅读:
    C语言学习笔记-静态库、动态库的制作和使用
    各种消息队列的对比
    如何使用Jupyter notebook
    Ubuntu16.04配置OpenCV环境
    Docker容器发展历史
    Ubuntu OpenSSH Server
    SpringBoot 系列文章
    SpringBoot 模板 Thymeleaf 的使用
    18、spring注解学习(AOP)——AOP功能测试
    17、spring注解学习(自动装配)——@Profile根据当前环境,动态的激活和切换一系列组件的功能
  • 原文地址:https://www.cnblogs.com/Seven-cjy/p/6101654.html
Copyright © 2011-2022 走看看