본문 바로가기

Android

셰이더 - ShapeDrawable

activity_main.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/LinearLayout1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

 

    <Button

        android:id="@+id/btn1"

        android:layout_width="90dp"

        android:layout_height="90dp"

        android:layout_margin="5dp"/>

   

     <Button

        android:id="@+id/btn2"

        android:layout_width="90dp"

        android:layout_height="90dp"

        android:layout_margin="5dp"/>

    

     <Button

        android:id="@+id/btn3"

        android:layout_width="90dp"

        android:layout_height="90dp"

        android:layout_margin="5dp"

        android:background="@drawable/shape3"/>

 

</LinearLayout>

 

Shape3.xml




 

<shape xmlns:android="http://schemas.android.com/apk/res/android"

     android:shape="oval">

    <solid android:color="#FFFFFFFF" />

    <stroke android:width="4px" android:color="#FF00FF00" />

</shape>

 

MainActivity.java

 

package com.example.viewex02;

 

import android.os.Bundle;

 

import android.app.Activity;

import android.graphics.Color;

import android.graphics.LinearGradient;

import android.graphics.Path;

import android.graphics.Shader.TileMode;

import android.graphics.drawable.GradientDrawable;

import android.graphics.drawable.ShapeDrawable;

import android.graphics.drawable.shapes.PathShape;

import android.view.Menu;

import android.widget.Button;

 

public class MainActivity extends Activity {

 

     @Override

     protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);

           setContentView(R.layout.activity_main);

          

           Button btn1 = (Button)findViewById(R.id.btn1);

          

           GradientDrawable gradientDrawable

           = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[]{Color.BLUE,Color.GREEN});

           btn1.setBackgroundDrawable(gradientDrawable);

          

           Button btn2= (Button)findViewById(R.id.btn2);

           Path path= new Path();

           path.moveTo(5, 0);

           path.lineTo(0, 7);

           path.lineTo(3, 7);

           path.lineTo(3, 10);

           path.lineTo(7, 10);

           path.lineTo(7, 7);

           path.lineTo(10, 7);

          ShapeDrawable sd3 = new ShapeDrawable(new PathShape(path,10,10));

           sd3.getPaint().setShader(new LinearGradient(0,0,0,10,

           0xff00ff00, 0xffff0000, TileMode.CLAMP));

           btn2.setBackgroundDrawable(sd3);

    

     }

 

     @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;

     }

 

}