Giter Club home page Giter Club logo

shapeableimageviewdemo's Introduction

ShapeableImageView Demo

This is a project to demonstrate how ShapeableImageView works.

ShapeableImageView supports two corner families CornerFamily.ROUNDED and CornerFamily.CUT. The code to change the shape is minimal and does not need different shape file, just add an overlay in styles.xml and then apply it using the app:shapeAppearanceOverlay attribute Use can also use kotlin to apply the desired shape and also change it dynamically.

Important Note app:shapeAppearanceOverlay is working as a ThemeOverlay. ShapeAppearanceOverlay applies the changes without affecting the active theme. THe key to achieve this behaviour is to leave parent attribute empty

Getting started

Add the dependency for the material library

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

Usage

There are two ways oif defining the shape. Either define overlays in xml or apply the changes dynamically in kotlin.

In xml

<style name="ShapeAppearanceOverlay.App.Circle" parent="">
    <item name="cornerSize">50%</item>
</style>

<style name="ShapeAppearanceOverlay.App.Rounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
</style>

<style name="ShapeAppearanceOverlay.App.BottomLeftTopRightCut" parent="">
    <item name="cornerFamilyTopRight">cut</item>
    <item name="cornerSizeTopRight">80dp</item>
    <item name="cornerFamilyBottomLeft">cut</item>
    <item name="cornerSizeBottomLeft">80dp</item>
</style>

and then

<com.google.android.material.imageview.ShapeableImageView
            android:id="@+id/imageView"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/sample_image"
            app:strokeWidth="4dp"
            app:strokeColor="@color/colorAccent"
            app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.App.Circle" />

In kotlin

val cornerSize = resources.getDimension(R.dimen.corner_size)
val shapeAppearanceModel = imageView.shapeAppearanceModel.toBuilder()
    .setAllCorners(CornerFamily.ROUNDED, cornerSize)
    .build()

imageView.shapeAppearanceModel = shapeAppearanceModel

val cornerSize = resources.getDimension(R.dimen.corner_size)
val shapeAppearanceModel = imageView.shapeAppearanceModel.toBuilder()
    .setTopRightCorner(CornerFamily.ROUNDED, cornerSize)
    .setTopLeftCorner(CornerFamily.CUT, cornerSize)
    .setBottomRightCorner(CornerFamily.ROUNDED, cornerSize)
    .setBottomLeftCorner(CornerFamily.CUT, cornerSize)
    .build()

imageView.shapeAppearanceModel = shapeAppearanceModel

shapeableimageviewdemo's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.