根据尺寸进行动画处理

时间:2020-10-16 19:42:35

标签: android kotlin android-jetpack-compose

我想根据固定值和组合的 $ width 为组合创建动画。 如何获得 $ width 以将其用于动画功能? 这是我的代码

@Composable
fun ExpandingCircle() {
    val (checked, setChecked) = remember { mutableStateOf(false) }
    val radius = if (checked) **$width** else 4.dp
    val radiusAnimated = animate(radius)
    Canvas(
        modifier = Modifier.fillMaxSize()
            .clickable(onClick = { setChecked(!checked) }),
        onDraw = {
            drawCircle(color = Color.Black, radius = radiusAnimated.toPx())
        }
    )
}

2 个答案:

答案 0 :(得分:1)

我们可以从DrawScope中获取尺寸,从尺寸中我们可以获取Canvas的宽度和高度,因此您可以像这样制作动画。

@Composable
fun ExpandingCircle() {
    val (checked, setChecked) = remember { mutableStateOf(false) }
    val unCheckedRadius = 4.dp
    Canvas(
        modifier = Modifier.fillMaxSize()
            .clickable(onClick = { setChecked(!checked) }),
        onDraw = {
            val width = size.width
            drawCircle(color = Color.Black, radius = if (checked) width/2 else unCheckedRadius.toPx())
        }
    )
}

答案 1 :(得分:0)

我意识到我不需要动画的宽度,但是我可以使用动画/插值浮点数将其用于DrawScope中的计算

    public class PizzaShop extends JFrame {

    String s1 []= {"First Name","Family","Telephone","Total Cost"};
    String s2[][]= {{"Base","Chicken","Meat"},{"Mozarella","Cheddar","Onion"},{"Tomato","Mushroom","Olive"}};
    JLabel jl[]= new JLabel [4];
    TextField tl[]=new TextField [4];
    JButton[][] buttons = new JButton[3][3];
    
    public PizzaShop () {

        for (int i=0; i<s1.length; i++) 
            jl[i]=new JLabel (s1[i]);
        for(int i=0;i<3;i++)
            for(int j=0; j<3;j++)
                buttons[i][j]=new JButton (s2[i][j]);
        
        // layout 
        GridLayout layout= new GridLayout (4,5);
        
        for(int i=0; i<3; i++) 
            add(jl[i]);
        for(int i=0; i<3; i++) 
            add(tl[i]);
        for(int i=0;i<3;i++)
            for(int j=0; j<3;j++)
                add(buttons[i][j]);

        setVisible(true);
        setSize(300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
                    }
        public static void main(String[] args) {
        PizzaShop order = new PizzaShop();
    }
}