簡體   English   中英

如何用圖片制作自定義形狀(形狀內部)

[英]How can make custom shape with picture (inside the shape)

我是 android 編程的新手,我嘗試設計這個形狀:

檢查此鏈接以查看我要設計的形狀

如何制作這種形狀,以及如何像亞歷山大一樣將圖片放入形狀中?

誰能幫我? 請回答所有細節以了解代碼

謝謝

在 build.gradle 中添加依賴

implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">
    
    <com.github.siyamed.shapeimageview.mask.PorterShapeImageView
        android:id="@+id/imageView"
        android:layout_width="250dp"
        android:layout_height="350dp"
        app:siShape="@drawable/shape" />
    
</LinearLayout>

活動

public class MainActivity extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView=findViewById(R.id.imageView);
        imageView.setImageResource(R.drawable.img);
    }
}

可繪制資源

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#FFC107" />
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners
        android:topLeftRadius="0dip"
        android:topRightRadius="70dip"
        android:bottomLeftRadius="70dip"
        android:bottomRightRadius="0dip" />
</shape>

輸出

輸出

您可以為此使用ShapeableImageView ,它在material組件包中提供。 如果你已經在你的項目中使用了材質主題,那么你就可以開始了,否則,添加material依賴。

implementation 'com.google.android.material:material:1.4.0'

並繼承,你AppThemeMaterialComponents ,在styles.xml

<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
     ...
     ...
</style>

將新樣式添加到style.xml以創建圖像的圓角。

<style name="ShapeAppearanceOverlay.RoundedCorner" parent="">
    <item name="cornerSizeTopRight">75dp</item>
    <item name="cornerFamilyTopRight">rounded</item>
    <item name="cornerSizeBottomLeft">75dp</item>
    <item name="cornerFamilyBottomLeft">rounded</item>
</style>

在布局ShapeableImageView ImageView更改為ShapeableImageView ,並使用app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.RoundedCorner"添加ShapeAppearnaceOverlay樣式到它上面

<com.google.android.material.imageview.ShapeableImageView
        android:layout_width="250dp"
        android:layout_height="350dp"
        android:src="@drawable/pots"
        android:scaleType="centerCrop"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.RoundedCorner"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toEncrypt"
        app:layout_constraintBottom_toTopOf="@id/decrypted"
        android:id="@+id/encrypted" />

注意- 使用android:scaleType="centerCrop" ,這樣您就不會在圖像的任一側獲得剪切效果。

結果——

結果

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM