【问题标题】:Bitmap image with rounded corners with stroke带笔画的圆角位图图像
【发布时间】:2013-01-23 05:35:30
【问题描述】:

我有一个边缘锐利的图像。

tile_mode.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background"
    android:tileMode="repeat">
</bitmap>

back.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">  
       <item android:drawable="@drawable/tile_mode" />
    <item>
        <shape>
            <solid/>
            <stroke android:width="1dip" android:color="#225786" />
            <corners android:radius="10dip"/>
            <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
        </shape>
    </item> 

layout.xml

<LinearLayout
                android:id="@+id/frame1"
                android:background="@drawable/back"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </LinearLayout>

我将图像设置为此布局的背景并为其绘制边框,但问题是图像是带有锐边的正方形,而我在 xml 中绘制的边框是圆角。那么如何让图片也有圆角呢?

【问题讨论】:

    标签: android image bitmap


    【解决方案1】:

    这是您必须 make round to your main layout background 并在里面保留您的 imageview with your desire image: 的解决方案之一

    如下所示:

    back.xml

    这会让你的图片变成圆角

    <?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
         <stroke android:width="1dp" android:color="#dd7b7a"/>
         <corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp" 
         android:topLeftRadius="10dp" android:topRightRadius="10dp"/> 
         <solid android:color="#dd7b7a"/>
     </shape>
    

    tile_mode.xml

    <?xml version="1.0" encoding="utf-8"?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/background"
    android:tileMode="repeat" />
    

    layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        >
    <LinearLayout 
         android:padding="4dip"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/back"
        android:gravity="center_horizontal"
        >
    <LinearLayout  
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
       android:background="@drawable/tile_mode"
        />
    </LinearLayout>  
    </LinearLayout>
    

    更新

    在挖了很多之后,我发现了一个已经发布在stackoverflow的解决方案

    Changing Image as Rounded Corner

    How to make an ImageView to have rounded corners

    step1@

    main.xml

     <RelativeLayout 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:gravity="center"
            tools:context=".MainActivity" >
    
            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
    
              />
    
        </RelativeLayout>
    

    第二步@

    使用画布制作一个对位图进行四舍五入的函数。

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                    .getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
    
            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            final RectF rectF = new RectF(rect);
            final float roundPx = pixels;
    
            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);
    
            return output;
        }
    

    第三步@

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ImageView image=(ImageView)findViewById(R.id.image);
    
    
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.testing);
    
    
        image.setImageBitmap(getRoundedCornerBitmap(bitmap, 20));
    

    【讨论】:

    • 仍然是图像边缘锐利,xml中没有位图背景\
    • 好的,现在我在哪里添加背景图像,我的意思是位图 xml?
    • 我的意思是这个 schemas.android.com/apk/res/android">
    • @Goofy 检查一下,你添加的方式和之前添加的一样。
    • 请稍等,我会检查一下,请帮帮我,我卡住了很长时间
    【解决方案2】:

    好的,我们试试下面的方法

         <?xml version="1.0" encoding="utf-8"?>
     <shape
        android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="30dp"/>
        <stroke android:width="2dp" android:color="#000000"/>
    </shape>
    
    <LinearLayout
        android:id="@+id/frame1"
        android:background="@drawable/corner_background"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        >
        <ImageView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/background" 
        />
    </LinearLayout>
    

    【讨论】:

    • 我将如何引用我的位图 xml ?
    • 您不需要使用您的位图图像,只需使用它会处理的我的 xml 图像文件名,您只需将您的位图存档即可。让我知道
    • 对于我发送给您的以下代码中的边缘更改更改
    • 你能帮我吗,我的意思是这个 ; 我应该在哪里添加这个?在你的代码中
    • ok,我们试试 schemas.android.com/apk/res/android">
    【解决方案3】:

    由于我收到有关变量分配的错误,RobinHood 的回答对我有用,但有一个更改。

    我不得不换行:

            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    

    到这一行:

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        
    

    使我的完整代码如下:

    public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
        setContentView(R.layout.activity_main);
    
        TextView textViewTitle = (TextView) findViewById(R.id.MYTEXTIDHERE);
        textViewTitle.setText("Some Text");
    
        ImageButton imageButtonSetter = (ImageButton) findViewById(R.id.MYIMAGEIDHERE);
        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.MYIMAGENAMEHERE);     
        imageButtonSetter.setImageBitmap(getRoundedCornerBitmap(myBitmap, 40));
    }   
    
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
    
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;
    
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        
        canvas.drawBitmap(bitmap, rect, rect, paint);
    
        return output;
    }
    

    【讨论】:

      【解决方案4】:

      实际上,我制作了一个完全符合您需求的库,您不必为那些 xml 文件而烦恼。

      https://github.com/pungrue26/SelectableRoundedImageView

      通过这个开源项目,您可以在每个角落设置不同的半径,并设置边框(宽度和颜色)等,如下所示。希望对你有帮助。

      【讨论】:

        【解决方案5】:

        将原始位图传递给以下函数,您将得到一个圆角位图:)。希望这可以帮助某人。

         public Bitmap getRoundedBitmap(Bitmap bitmap) {
                Bitmap resultBitmap;
                int originalWidth = bitmap.getWidth();
                int originalHeight = bitmap.getHeight();
                float r;
        
                if (originalWidth > originalHeight) {
                    resultBitmap = Bitmap.createBitmap(originalHeight, originalHeight,
                            Bitmap.Config.ARGB_8888);
                    r = originalHeight / 2;
                } else {
                    resultBitmap = Bitmap.createBitmap(originalWidth, originalWidth,
                            Bitmap.Config.ARGB_8888);
                    r = originalWidth / 2;
                }
        
                Canvas canvas = new Canvas(resultBitmap);
        
                final Paint paint = new Paint();
                final Rect rect = new Rect(ConstsCore.ZERO_INT_VALUE,
                        ConstsCore.ZERO_INT_VALUE, originalWidth, originalHeight);
        
                paint.setAntiAlias(true);
                canvas.drawARGB(ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE,
                        ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE);
                canvas.drawCircle(r, r, r, paint);
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                canvas.drawBitmap(bitmap, rect, rect, paint);
        
                return resultBitmap;
            }
        

        【讨论】:

          【解决方案6】:

          试试你的 back.xml 类似的东西。

          <?xml version="1.0" encoding="UTF-8"?>
          
          <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
          <solid android:color="#ffffffff"/>    
          
          <stroke android:width="3dp"
                  android:color="#ff000000"
                  />
          
          <padding android:left="1dp"
                   android:top="1dp"
                   android:right="1dp"
                   android:bottom="1dp"
                   /> 
          
          <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
           android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
          </shape>
          

          【讨论】:

          • 在哪里添加tilemode重复的位图xml?
          【解决方案7】:

          我今天遇到了类似的问题,只是我的图片是谷歌地图。你实际上必须隐藏角落,这样做的方法是创建一个 9Patch,如下所示:

          并将其作为背景应用于您的所有布局或覆盖您的布局的另一个布局。 请查看以下链接以获取更多信息:

          Is there a way to implement rounded corners to a Mapfragment?

          我已经访问了这个网站:http://www.sumopaint.com/app/
          并全部手工绘制,您可以以我的为例并根据自己的喜好更改颜色。 你可能需要下一个:

          您可以在以下链接中获得有关如何创建 9Patch 的更多信息:

          http://radleymarx.com/blog/simple-guide-to-9-patch/

          http://android9patch.blogspot.co.il/

          【讨论】:

          • 怎么做,请指导我,我卡住了很长时间
          • 嘿怎么做我需要创建一个这样的图像并用这个图像隐藏它?
          • 四角的颜色是什么?白色的?
          • 那么这种方式对你没有帮助。
          【解决方案8】:
          <?xml version="1.0" encoding="utf-8"?>
          

          <item>
              <bitmap
                  android:src="@drawable/bg_striped_img"
                  android:tileMode="repeat" />
          </item>
          <item android:left="-20dp" android:top="-20dp" android:right="-20dp" android:bottom="-20dp">
              <shape android:shape="oval" >
                  <stroke
                      android:width="20dp"
                      android:color="#ffffffff" />
          
                  <solid android:color="#00000000" />
          
                  <size
                      android:height="120dp"
                      android:width="120dp" />
              </shape>
          </item>
          
              <item >
              <shape android:shape="oval" >
                  <stroke
                      android:width="1dp"
                      android:color="#ff999999" />
          
                  <solid android:color="#00000000" />
          
                  <size
                      android:height="120dp"
                      android:width="120dp" />
              </shape>
          </item>
          

          【讨论】:

            猜你喜欢
            • 2014-11-10
            • 2013-08-16
            • 2018-05-24
            • 2021-05-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多