zoukankan      html  css  js  c++  java
  • android 和 java 调色板

    大二的时候用java编过一个调色板,现在把它移植到了android上。在java中我是用按键监听的方法改变TextArea的颜色,android中,我是用SeekBar组件拖动改变view里的颜色

    先给大家看个在java中的调色板,比较简单,适合刚入门编写这个程序

    View Code
      1   import java.awt.BorderLayout;
    2 import java.awt.Button;
    3 import java.awt.Color;
    4 import java.awt.Frame;
    5 import java.awt.GridLayout;
    6 import java.awt.Label;
    7 import java.awt.Panel;
    8 import java.awt.TextField;
    9 import java.awt.event.ActionEvent;
    10 import java.awt.event.ActionListener;
    11 import java.awt.event.WindowAdapter;
    12 import java.awt.event.WindowEvent;
    13
    14 public class syj {
    15 //static变量是静态变量,好处是程序中运行生成多少个该类的对象,它们都共享该变量
    16 static int r = 0, g = 250, b = 0;
    17 public static void main(String args[]) {
    18 //是已(100,100)为坐标原点,长我600,高为500的Frame
    19 Frame myframe2 = new Frame("我的调色板");
    20 myframe2.setSize(600,400);
    21 myframe2.setLocation(100, 100);
    22 //对myframe2进行监听
    23 myframe2.addWindowListener(new WindowAdapter(){
    24 public void windowClosing(WindowEvent e){
    25 System.exit(0);
    26 }
    27 });
    28 //设定三个标签,分别为红,绿,蓝,标签字体都居中
    29 Label lb1 = new Label("红色");
    30 Label lb2 = new Label("绿色");
    31 Label lb3 = new Label("蓝色");
    32 lb1.setAlignment(Label.CENTER);
    33 lb2.setAlignment(Label.CENTER);
    34 lb3.setAlignment(Label.CENTER);
    35 lb1.setBackground(Color.red);
    36 lb2.setBackground(Color.green);
    37 lb3.setBackground(Color.blue);
    38 //设置六个Button
    39 Button rbt1 =new Button("+");
    40 Button rbt2 =new Button("-");
    41 Button gbt1 =new Button("+");
    42 Button gbt2 =new Button("-");
    43 Button bbt1 =new Button("+");
    44 Button bbt2 =new Button("-");
    45 //第一个参数是指显示在TextField上的值是0,第二个参数是指TextField长度为8
    46 final TextField tf1 = new TextField("0",8);
    47 final TextField tf2 = new TextField("250",8);
    48 final TextField tf3 = new TextField("0",8);
    49
    50 Panel p0 = new Panel();
    51 //final对象就是说在整个函数体中此对象是不能被修改的,是只读的。
    52 //而对于这个对象本身是可以 改变的
    53 final Panel p1 = new Panel();
    54 //对p0上的组件进行网格状排布,第一个参数是行数,第二个是列数
    55 //第三个是水平间隔,第四个是垂直间隔
    56 p0.setLayout(new GridLayout(3,4,8,8));
    57 //把组件都放到panel上
    58 p0.add(lb1);
    59 p0.add(rbt1);
    60 p0.add(tf1);
    61 p0.add(rbt2);
    62
    63 p0.add(lb2);
    64 p0.add(gbt1);
    65 p0.add(tf2);
    66 p0.add(gbt2);
    67
    68 p0.add(lb3);
    69 p0.add(bbt1);
    70 p0.add(tf3);
    71 p0.add(bbt2);
    72 //p0.setVisible(true);
    73
    74 p1.setBackground(new Color(r,g,b));
    75 //第一个参数是垂直间隔,第二个参数是水平间隔
    76 myframe2.setLayout(new BorderLayout(20,20));
    77 myframe2.add(p0,"North");
    78 myframe2.add(p1,"Center");
    79 myframe2.setVisible(true);
    80
    81 //分别对每个button都进行监听,并处理相应事件
    82 rbt1.addActionListener(new ActionListener(){
    83 public void actionPerformed(ActionEvent e){
    84 if(r >= 0&& r < 254)
    85 r++;
    86 else
    87 r=0;
    88 tf1.setText(" "+r);
    89 p1.setBackground(new Color(r,g,b));
    90 }
    91 });
    92
    93 rbt2.addActionListener(new ActionListener(){
    94 public void actionPerformed(ActionEvent e){
    95 if(r>0)
    96 r--;
    97 else
    98 if(r==0)
    99 r=255;
    100 tf1.setText(" "+r);
    101 p1.setBackground(new Color(r,g,b));
    102 }
    103 });
    104
    105 gbt1.addActionListener(new ActionListener(){
    106 public void actionPerformed(ActionEvent e){
    107 if(g >= 0&& g < 254)
    108 g++;
    109 else
    110 g=0;
    111 tf2.setText(" "+g);
    112 p1.setBackground(new Color(r,g,b));
    113 }
    114 });
    115
    116 gbt2.addActionListener(new ActionListener(){
    117 public void actionPerformed(ActionEvent e){
    118 if(g>0)
    119 g--;
    120 else
    121 if(g==0)
    122 g=255;
    123 tf2.setText(" "+g);
    124 p1.setBackground(new Color(r,g,b));
    125 }
    126 });
    127
    128 bbt1.addActionListener(new ActionListener(){
    129 public void actionPerformed(ActionEvent e){
    130 if(b >= 0&& b < 254)
    131 b++;
    132 else
    133 b=0;
    134 tf3.setText(" "+b);
    135 p1.setBackground(new Color(r,g,b));
    136 }
    137 });
    138
    139 bbt2.addActionListener(new ActionListener(){
    140 public void actionPerformed(ActionEvent e){
    141 if(b>0)
    142 b--;
    143 else
    144 if(b==0)
    145 b=255;
    146 tf3.setText(" "+b);
    147 p1.setBackground(new Color(r,g,b));
    148 }
    149 });
    150 }
    151 }

    下面是效果图:

    第二个是android里编的

    第一步:先写main.xml文件,比较简单,一看就会

    View Code
     1 <?xml version="1.0" encoding="utf-8"?>
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:orientation="vertical"
    4 android:layout_width="fill_parent"
    5 android:layout_height="fill_parent"
    6 android:weightSum="1">
    7 <View
    8 android:id="@+id/view1"
    9 android:layout_width="wrap_content" android:layout_height="386dp" android:layout_weight="1.14"></View>
    10 <SeekBar
    11 android:id="@+id/seek1"
    12 android:layout_width="fill_parent"
    13 android:layout_height="wrap_content"
    14 android:max="255"/>
    15 <SeekBar
    16 android:id="@+id/seek2"
    17 android:layout_width="fill_parent"
    18 android:layout_height="wrap_content"
    19 android:max="255"
    20 />
    21 <SeekBar
    22 android:id="@+id/seek3"
    23 android:layout_width="fill_parent"
    24 android:layout_height="wrap_content"
    25 android:max="255"/>
    26 </LinearLayout>


    第二步编写Tiaoseban.java文件,主要是对三个SeekBar进行监听改变View的颜色

    View Code
     1 package shao.tiaoseban;
    2
    3 import android.app.Activity;
    4 import android.graphics.Color;
    5 import android.os.Bundle;
    6 import android.view.View;
    7 import android.widget.SeekBar;
    8 import android.widget.SeekBar.OnSeekBarChangeListener;
    9
    10 //实现了SeekBar.OnSeekBarChangeListener这个接口
    11 public class TiaosebanActivity extends Activity implements SeekBar.OnSeekBarChangeListener{
    12 //定义三个静态变量
    13 static int r = 0,g = 0 ,b = 0;
    14 //声明view对象
    15 private View view;
    16 //声明三个seekbar对象
    17 private SeekBar seekbar1;
    18 private SeekBar seekbar2;
    19 private SeekBar seekbar3;
    20
    21 @Override
    22 public void onCreate(Bundle savedInstanceState) {
    23 super.onCreate(savedInstanceState);
    24 setContentView(R.layout.main);
    25 //得到View对象
    26 view = (View)findViewById(R.id.view1);
    27 //给view显示背景颜色
    28 view.setBackgroundColor(Color.rgb(r, g, b));
    29 //得到三个seekbar对象
    30 seekbar1 = (SeekBar)findViewById(R.id.seek1);
    31 seekbar2 = (SeekBar)findViewById(R.id.seek2);
    32 seekbar3 = (SeekBar)findViewById(R.id.seek3);
    33 //分别对三个seek绑定监听器
    34 seekbar1.setOnSeekBarChangeListener( (OnSeekBarChangeListener) this);
    35 seekbar2.setOnSeekBarChangeListener( (OnSeekBarChangeListener) this);
    36 seekbar3.setOnSeekBarChangeListener( (OnSeekBarChangeListener) this);
    37 }
    38 //seekbar拖动时实现这个方法
    39 public void onProgressChanged(SeekBar seekbar2,int progress, boolean fromTouch){
    40 r = seekbar1.getProgress();
    41 g = seekbar2.getProgress();
    42 b = seekbar3.getProgress();
    43 view.setBackgroundColor(Color.rgb(r, g, b));
    44 }
    45
    46 public void onStartTrackingTouch (SeekBar seekbar){
    47
    48 }
    49 //停止拖动
    50 public void onStopTrackingTouch (SeekBar seekbar){
    51
    52 }
    53
    54 }

  • 相关阅读:
    [Xcode 实际操作]八、网络与多线程-(16)使用网址会话对象URLSession下载图片并显示下载进度
    [Xcode 实际操作]八、网络与多线程-(15)使用网址会话对象URLSession下载图片并存储在沙箱目录中
    leetcode第一刷_Best Time to Buy and Sell Stock II
    Unity 武器拖尾效果
    Android环境搭建 NDK+ADT(免cywgin)
    ros下单目相机校正
    Valid Phone Numbers
    ExtJS4.2.1与Spring MVC实现Session超时控制
    Kafka具体解释二、怎样配置Kafka集群
    OutOfMemoryError: Java heap space和GC overhead limit exceeded在Ant的Build.xml中的通用解决方式
  • 原文地址:https://www.cnblogs.com/shaoyangjiang/p/2371093.html
Copyright © 2011-2022 走看看