
[Android]Layoutの背景色を動的に変更するサンプル#tryShapeColor00


Layoutで作った背景色を変更するようにする。
drawableで指定したレイアウトを変更しないようにする。
setBackgroundColorで変更するとレイアウトが壊れて指定した色で塗りつぶされてしまう。
Layoutのグラデーションを動的に変更する。
検索した事
Android レイアウト 背景色
Android 背景色 動的
Android getBackground Drawable
開発環境
Eclipse IDE バージョン: 4.2.1
ターゲットプラットフォーム: 2.3.3
API レベル: 10
package com.example.tryshapecolor00; import java.util.Random; import android.app.Activity; import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.GradientDrawable.Orientation; import android.os.Bundle; import android.view.Menu; import android.view.View; public class TryShapeColor00 extends Activity { private Random mRandom = new Random(); // ランダム @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_try_shape_color00); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_try_shape_color00, menu); return true; } /** * ミスターボタンが押されたときに呼び出される * @param view ビュー */ public void onClickMrButton(View view) { // ビューの色を変える this.changeViewColor(); // ビューのグラデーションを変える this.changeGradViewColor(); } /** * ビューの色の変更 */ public void changeViewColor() { // ビュー final View view = this.findViewById(R.id.view1); // 背景色を変える // 直接view.setBackgroundColorをすると全部が塗りつぶされて作ったshapeが壊れてしまう GradientDrawable background = (GradientDrawable)view.getBackground(); background.setColor(0xff000000 | this.mRandom.nextInt(0xffffff)); } /** * グラデーションがかかったビューの色の変更 */ public void changeGradViewColor() { // ビュー final View view = this.findViewById(R.id.view2); GradientDrawable background = (GradientDrawable)view.getBackground(); // 新しいグラデーションの作成 int randColor = this.mRandom.nextInt(0xffffff); GradientDrawable newGrad = new GradientDrawable( Orientation.LEFT_RIGHT, new int[]{0xff000000 | randColor, randColor | 0x7f000000, randColor}); newGrad.setCornerRadii(new float[]{5, 5, 5, 5 }); view.setBackgroundDrawable(newGrad); } }
メインアクティビティのレイアウト
<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:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dip" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:background="@drawable/button_bg" android:onClick="onClickMrButton" android:text="Mrボタン" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <View android:id="@+id/view1" android:layout_width="wrap_content" android:layout_height="44dp" android:layout_marginBottom="5dip" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:background="@drawable/layout_bg" /> <View android:id="@+id/view2" android:layout_width="wrap_content" android:layout_height="44dp" android:layout_marginBottom="5dip" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:background="@drawable/layout_grad_bg" /> </LinearLayout> </LinearLayout>
背景に割り当てるXMLファイル
ボタン
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <item android:state_pressed="true"> <shape android:shape="rectangle"> <gradient android:startColor="#fffbcf" android:endColor="#e6e2bb" android:angle="90" /> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="2dp" android:topRightRadius="2dp" /> <stroke android:width="1px" android:color="#8e6b47" /> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp"/> </shape> </item> <item android:state_focused="true"> <shape android:shape="rectangle"> <gradient android:startColor="#fffbcf" android:endColor="#e6e2bb" android:angle="90" /> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="2dp" android:topRightRadius="2dp" /> <stroke android:width="1px" android:color="#8e6b47" /> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp"/> </shape> </item> <item> <shape> <gradient android:startColor="#e3e3e5" android:endColor="#bebebf" android:angle="90" /> <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" android:topLeftRadius="2dp" android:topRightRadius="2dp" /> <stroke android:width="1px" android:color="#8e6b47" /> <padding android:left="7dp" android:top="7dp" android:right="7dp" android:bottom="7dp"/> </shape> </item> </selector>
ビュー1
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffff0000" /> <corners android:bottomRightRadius="50dp" android:bottomLeftRadius="50dp" android:topLeftRadius="20dp" android:topRightRadius="20dp" /> </shape>
ビュー2
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffff0000" /> <gradient android:angle="315" android:startColor="#ff000000" android:centerColor="#00000000" android:endColor="#00000000" android:type="linear" android:centerX="0.5" android:centerY="0.5" android:gradientRadius="10" /> </shape>
[Android]ViewPagerを使って画面をスワイプ、フリックするサンプル#tryVie... [Android]TextViewが重いと感じたあなたのためのLabelViewサンプル#tryLab...