简体   繁体   English

在运行时更改Imageview的角半径

[英]Change corner radius of Imageview at runtime

hello guys Here is my xml file. 大家好,这是我的xml文件。 I want to change corner radius of Imageview at run time in such a way corner radius of bitmap in Imageview should be changed . 我想在运行时更改Imageview的拐角半径,以这种方式应更改Imageview中位图的拐角半径。 Does anyone have idea that how can I perform this task. 有谁知道我该如何执行此任务。

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
   android:id="@+id/mainContainer"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:gravity="center"
   >

 <TableLayout 
     android:id="@+id/firstpicTable"
     android:layout_width="102.5dp"
     android:layout_height="200dp"
     android:background="@drawable/tableborder"
     android:layout_marginTop="5dp"
     android:padding="0.5dp"
     android:layout_marginBottom="5dp"
             >
              <ImageView
         android:id="@+id/image1"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:scaleType="fitXY"

         android:background="@android:color/white"
         android:contentDescription="@string/app_name"
         />


 </TableLayout>

  <TableLayout 
     android:id="@+id/secondtpicTable"
     android:layout_width="102.5dp"
     android:layout_height="200dp"
     android:layout_marginTop="5dp"
      android:background="@drawable/tableborder"
      android:padding="0.5dp"

     android:layout_toRightOf="@+id/firstpicTable"
     >

     <ImageView 
         android:id="@+id/image223"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
          android:focusable="true"
         android:background="@android:color/white"
         android:contentDescription="@string/app_name"
         android:scaleType="fitXY"

         />
 </TableLayout>

   <TableLayout 
     android:id="@+id/thirddtpicTable"
     android:layout_width="102.5dp"
     android:layout_height="200dp"
     android:layout_marginTop="5dp"

     android:layout_toRightOf="@+id/secondtpicTable"
      android:padding="0.5dp"
      android:background="@drawable/tableborder"

     android:gravity="center">
     <ImageView 
         android:id="@+id/image3"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@android:color/black"
         android:scaleType="fitXY"
         android:focusable="true"
         android:contentDescription="@string/app_name"

         />
 </TableLayout>

Like Romain Guy wrote in his blog [1], you can draw a rounded image by implementing a Drawable and use canvas.drawRoundRect() 就像罗曼·盖伊(Romain Guy)在其博客[1]中所写的那样,您可以通过实现Drawable并使用canvas.drawRoundRect()来绘制圆形图像。

If you want to vary the corner radius at runtime, then you can update a property in your drawable (a integer like mCornerRadius for instance), and invoke invalidateSelf() to trigger the redraw. 如果要在运行时更改拐角半径,则可以更新可绘制对象中的属性(例如,类似于mCornerRadius的整数),然后调用invalidateSelf()来触发重绘。

[1] http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/ [1] http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

Why don't you make shape in drawable and use in your ImageView like this 为什么不像这样在drawable中制作形状并在ImageView中使用

round_shape.xml round_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#ffffff"/>    

    <stroke android:width="3dp"
            android:color="#ff000000"/>

    <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp"/> 

    <corners android:radius="30dp"/> 
</shape>

Provide your own custom radius value under corners tag corners标签下提供您自己的自定义半径值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM