前些天做了一个android的小小的project——Modern Art UI,通过滑动SeekBar来改变矩形的颜色。
背景:暑假期间,学校安排了国际实践周的课程,我花了4天时间学习了《OverView of the android platform》这门课,课程结束时,老师要问们完成一个android project,参照结果给了一个样式:
下面分析一下如何android中颜色值是怎么比表示的吧:
#RGB:分别指定红、绿、蓝三原色的值(只支持0~f这16级颜色)来代表颜色;
#ARGB:分别指定红、绿、蓝三原色的值(只支持0~f这16级颜色)及透明度(只支持0~f这16级透明度)来代表颜色;
#RRGGBB:分别指定红、绿、蓝三原色的值(支持00~ff这256级颜色)来代表颜色;
#AARRGGBB:分别指定红、绿、蓝三原色的值(支持00~fff这256级颜色)及透明度(支持00~fff这256级透明度)来代表颜色。
上面四种形式中,A、R、G、B都代表一个十六进制的数,其中A代表透明度,R代表红色的数值,G代表绿色数值,B代表蓝色数值。
下面直接上干货——代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:weightSum="1"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_weight="0.9"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:orientation="vertical"
android:layout_weight="0.6" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="198dp"
android:background="#FFFF5d00" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="198dp"
android:layout_marginTop="5dp"
android:background="#FF0000FF" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:layout_marginLeft="5dp"
android:orientation="vertical"
android:layout_weight="0.4">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="128dp"
android:background="#FFFAA840" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="128dp"
android:layout_marginTop="5dp"
android:background="#FFFFFFFF"
/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="128dp"
android:layout_marginTop="5dp"
android:background="#FFFF0000" />
</LinearLayout>
</LinearLayout>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_gravity="bottom"
android:max="100"
android:progress="0"
android:secondaryProgress="100" />
</LinearLayout>
</RelativeLayout>
java文件里的代码:
package com.example.modernartui;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.SeekBar;
public class MainActivity extends Activity implements
SeekBar.OnSeekBarChangeListener{
ImageView imageview1,imageview2,imageview3,imageview4,imageview5;
SeekBar seekbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview1 = (ImageView)findViewById(R.id.imageView1);
imageview2 = (ImageView)findViewById(R.id.imageView2);
imageview3 = (ImageView)findViewById(R.id.imageView3);
imageview4 = (ImageView)findViewById(R.id.imageView4);
imageview5 = (ImageView)findViewById(R.id.imageView5);
seekbar = (SeekBar)findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
imageview1.setBackgroundColor(Color.argb(255, 190-arg1, arg1+150, arg1+155));
imageview2.setBackgroundColor(Color.argb(255, arg1+141, arg1+180, 2555-arg1));
imageview3.setBackgroundColor(Color.argb(255, arg1+50, arg1+90, arg1+155));
imageview5.setBackgroundColor(Color.argb(255, 255-arg1, arg1+20, arg1+100));
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
new AlertDialog.Builder(this).setTitle("Corporation").setMessage("Maded by XX XXXX级,学号XXXXXXXXXXXXX").setPositiveButton("ok", null).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
最终运行截图: