【问题标题】:How to use BitMap in Android for following design?如何在 Android 中使用 BitMap 进行以下设计?
【发布时间】:2015-10-24 06:29:46
【问题描述】:

我刚开始使用Bitmap,想尝试设计一下这个按钮:

我正在使用SpannableString 进行进一步处理。如何使用Bitmap 获得这种带圆角的形状?

【问题讨论】:

    标签: java android canvas bitmap android-bitmap


    【解决方案1】:

    试试这个,

    活动中的位图使用

    package com.example.testproject;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.widget.ImageView;
    
    public class TestImages extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ImageView image = (ImageView) findViewById(R.id.test_image);
            Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
            image.setImageBitmap(bMap);
        }
    }
    

    对于圆形图像,请尝试以下代码

    rounded_corner.xml

        <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <!-- view background color -->
        <solid android:color="#ffffff" >
        </solid>
    
        <!-- view border color and width -->
        <stroke
            android:width="1dp"
            android:color="#1c1b20" >
        </stroke>
    
        <!-- If you want to add some padding -->
        <padding
            android:bottom="4dp"
            android:left="4dp"
            android:right="4dp"
            android:top="4dp" >
        </padding>
    
        <!-- Here is the corner radius -->
        <corners android:radius="10dp" >
        </corners>
    
    </shape>
    

    将此文件添加到您的可绘制文件夹中

    ma​​in_activity.xml

    将此行添加到您的 XML 文本视图中

    android:background="@drawable/rounded_corner"
    

    【讨论】:

    • 我没有 ImageView,正如您在帖子中看到的那样,我正在使用 spannable String only
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多