在android中创建一个3d形状的按钮

时间:2022-03-16 23:56:22


I was trying to create a button similar in look to the round buttons over here -
http://livetools.uiparade.com/index.html
(every button looks like it is inside an immersed section) I had it by placing the button
in a circle background and giving them both a little gradient that didnt end up the same
though I got this result -

在android中创建一个3d形状的按钮

(I will upload my code once I can) how can I achieve that same look?

我试图创建一个按钮看起来类似于圆形按钮在这里——http://livetools.uiparade.com/index.html(每个按钮看起来是在一个沉浸部分)我已经把一个圆的按钮背景和给他们一个小梯度,最终没有相同的虽然我得到这个结果,(我会上传我的代码一次)我怎么能实现同样的看?

2 个解决方案

#1


24  

Try this code. I am able to produce an image that looks like this

试试这个代码。我可以生成一个像这样的图像。

在android中创建一个3d形状的按钮

which is similar to the first button you link to, using the following code. The key is to use <layer-list> to layer shapes one over the other to produce the desired effect.

这与使用以下代码链接到的第一个按钮类似。关键是使用 到层的形状来产生想要的效果。

File: res/drawable/button.xml

文件:res /可拉的/ button.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <!-- Outside border/shadow -->
  <item>
    <shape android:shape="oval">
      <size android:width="200dp" android:height="200dp" />
      <gradient android:angle="90" android:startColor="#f4f4f4" android:endColor="#b9b9b9" />
    </shape>
  </item>

  <!-- Inset -->
  <item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
      <shape android:shape="oval">
        <gradient android:angle="90" android:startColor="#dcdcdc" android:endColor="#c9c9c9" />
      </shape>
  </item>

  <!-- Inside border/shadow -->
  <item android:top="15dp" android:left="15dp" android:right="15dp" android:bottom="15dp">
    <shape android:shape="oval">
      <gradient android:angle="90" android:startColor="#8c8c8c" android:endColor="#cbcbcb" />
    </shape>
  </item>

  <!-- Main button -->
  <item android:top="16dp" android:left="16dp" android:right="16dp" android:bottom="16dp">
    <shape android:shape="oval">
      <solid android:color="#ffffff" />
    </shape>
  </item>

  <!-- Button image -->
  <item android:top="70dp" android:left="70dp" android:right="70dp" android:bottom="70dp">
    <shape android:shape="rectangle">
      <solid android:color="#3b88c2" />
      <corners android:radius="20dp" />
    </shape>
  </item>

  <item android:top="75dp" android:left="75dp" android:right="75dp" android:bottom="75dp">
    <shape android:shape="rectangle">
      <solid android:color="#ffffff" />
      <corners android:radius="20dp" />
    </shape>
  </item>

  <item android:top="80dp" android:left="80dp" android:right="80dp" android:bottom="80dp">
    <shape android:shape="rectangle">
      <solid android:color="#3b88c2" />
      <corners android:radius="20dp" />
    </shape>
  </item>

</layer-list>

In your main layout, add an ImageView that will display this image.

在主布局中,添加一个将显示此图像的ImageView。

<ImageView
    android:src="@drawable/button" />

You can make the ImageView clickable by giving it an OnClickListener in the Java code.

通过在Java代码中为ImageView提供一个OnClickListener,可以使它成为可单击的。

#2


4  

Go to this link and Generate Custom 3D button.

转到此链接并生成自定义3D按钮。

http://angrytools.com/android/button/

http://angrytools.com/android/button/

buttonshape.xml

buttonshape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="30dp"
/>
<gradient
android:gradientRadius="45"
android:centerX="35%"
android:centerY="50%"
android:startColor="##4CAB0B"
android:endColor="#004507"
android:type="radial"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#0B8717"
/>
</shape>

Button Code

按钮的代码

<Button
android:id="@+id/angry_btn"

android:text="Button"
android:textColor="#FFFFFF"
android:textSize="30sp"

android:layout_width="270dp"
android:layout_height="60dp"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="3"
android:shadowDy="2"
android:shadowRadius="8"
/>

#1


24  

Try this code. I am able to produce an image that looks like this

试试这个代码。我可以生成一个像这样的图像。

在android中创建一个3d形状的按钮

which is similar to the first button you link to, using the following code. The key is to use <layer-list> to layer shapes one over the other to produce the desired effect.

这与使用以下代码链接到的第一个按钮类似。关键是使用 到层的形状来产生想要的效果。

File: res/drawable/button.xml

文件:res /可拉的/ button.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <!-- Outside border/shadow -->
  <item>
    <shape android:shape="oval">
      <size android:width="200dp" android:height="200dp" />
      <gradient android:angle="90" android:startColor="#f4f4f4" android:endColor="#b9b9b9" />
    </shape>
  </item>

  <!-- Inset -->
  <item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
      <shape android:shape="oval">
        <gradient android:angle="90" android:startColor="#dcdcdc" android:endColor="#c9c9c9" />
      </shape>
  </item>

  <!-- Inside border/shadow -->
  <item android:top="15dp" android:left="15dp" android:right="15dp" android:bottom="15dp">
    <shape android:shape="oval">
      <gradient android:angle="90" android:startColor="#8c8c8c" android:endColor="#cbcbcb" />
    </shape>
  </item>

  <!-- Main button -->
  <item android:top="16dp" android:left="16dp" android:right="16dp" android:bottom="16dp">
    <shape android:shape="oval">
      <solid android:color="#ffffff" />
    </shape>
  </item>

  <!-- Button image -->
  <item android:top="70dp" android:left="70dp" android:right="70dp" android:bottom="70dp">
    <shape android:shape="rectangle">
      <solid android:color="#3b88c2" />
      <corners android:radius="20dp" />
    </shape>
  </item>

  <item android:top="75dp" android:left="75dp" android:right="75dp" android:bottom="75dp">
    <shape android:shape="rectangle">
      <solid android:color="#ffffff" />
      <corners android:radius="20dp" />
    </shape>
  </item>

  <item android:top="80dp" android:left="80dp" android:right="80dp" android:bottom="80dp">
    <shape android:shape="rectangle">
      <solid android:color="#3b88c2" />
      <corners android:radius="20dp" />
    </shape>
  </item>

</layer-list>

In your main layout, add an ImageView that will display this image.

在主布局中,添加一个将显示此图像的ImageView。

<ImageView
    android:src="@drawable/button" />

You can make the ImageView clickable by giving it an OnClickListener in the Java code.

通过在Java代码中为ImageView提供一个OnClickListener,可以使它成为可单击的。

#2


4  

Go to this link and Generate Custom 3D button.

转到此链接并生成自定义3D按钮。

http://angrytools.com/android/button/

http://angrytools.com/android/button/

buttonshape.xml

buttonshape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="30dp"
/>
<gradient
android:gradientRadius="45"
android:centerX="35%"
android:centerY="50%"
android:startColor="##4CAB0B"
android:endColor="#004507"
android:type="radial"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
<stroke
android:width="3dp"
android:color="#0B8717"
/>
</shape>

Button Code

按钮的代码

<Button
android:id="@+id/angry_btn"

android:text="Button"
android:textColor="#FFFFFF"
android:textSize="30sp"

android:layout_width="270dp"
android:layout_height="60dp"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="3"
android:shadowDy="2"
android:shadowRadius="8"
/>