【问题标题】:CardView dynamic shadow depends not only on elevationCardView 动态阴影不仅仅依赖于高程
【发布时间】:2015-09-12 02:31:07
【问题描述】:

在开发我的应用程序时,我创建了一个内部带有可扩展 CardViews 的 RecyclerView,然后提到 “嘿,为什么当我只为卡片的垂直大小和扩展的不透明度设置动画时,海拔会发生一些变化?” .

然后我发现改变的不是海拔——它只是改变了一个影子。

有人听说过吗?只是界面绘制的一个未描述的事情吗?我对很酷的功能没有异议,但这让我有点困惑(我什至花了大约一个小时在 Google 的指南中找到一些关于它的信息)

我已经分享了证明“高度影响阴影绘制”这一想法的代码,但如果还有更多信息,请分享一些信息。

【问题讨论】:

    标签: android android-cardview


    【解决方案1】:

    在 Material Design 中有 2 个光源:

    • 主光 - 屏幕上方约 45°(头顶上方)的锐利光源
    • 环境光 - 垂直于屏幕(在您的头部后面)的更柔和和更弱的光源

    主光的阴影占主导地位。

    此模型在 Android UI 中实现。现在,来自 key light 的光线以与底部元素不同的角度照射靠近屏幕顶部的元素,因此阴影会相应地以不同的角度投射,就像在现实中一样- Material Design 背后的意图。

    实际上:元素越靠近底部,元素底部边缘的阴影越强,顶部边缘的阴影越亮。

    Material Design: Environment: Light and shadows: Light

    图片链接自:https://touchlab.co/2016-1-consistent-lighting-in-material-design/

    【讨论】:

      【解决方案2】:

      activity_main.xml

      <FrameLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent" android:layout_height="match_parent">
          <android.support.v7.widget.CardView android:id="@+id/dumb_card"
                                              android:layout_margin="16dp"
                                              tools:cardCornerRadius="4dp"
                                              android:layout_width="match_parent" android:layout_height="wrap_content">
              <LinearLayout android:orientation="vertical"
                            android:layout_width="match_parent" android:layout_height="match_parent">
      
                  <SeekBar android:id="@+id/heighter"
                           android:layout_margin="16dp"
                           android:layout_width="match_parent" android:layout_height="wrap_content"/>
      
                  <SeekBar android:id="@+id/elevator"
                           android:layout_margin="16dp"
                           android:layout_width="match_parent" android:layout_height="wrap_content"/>
      
              </LinearLayout>
          </android.support.v7.widget.CardView>
      </FrameLayout>
      

      MainActivity.java

      public class MainActivity extends Activity {
          CardView card;
          SeekBar  heighter;
          SeekBar  elevator;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              //card
              card = (CardView) findViewById(R.id.dumb_card);
      
              //card height
              heighter = (SeekBar) findViewById(R.id.heighter);
              heighter.setMax(2500);
              heighter.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                      ViewGroup.LayoutParams lp = card.getLayoutParams();
                      lp.height = progress;
                      card.setLayoutParams(lp);
                  }
                  public void onStartTrackingTouch(SeekBar seekBar) {}
                  public void onStopTrackingTouch(SeekBar seekBar) {}
              });
      
              //card elevation
              elevator = (SeekBar) findViewById(R.id.elevator);
              elevator.setMax(250);
              elevator.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                      card.setCardElevation(progress);
                  }
                  public void onStartTrackingTouch(SeekBar seekBar) {}
                  public void onStopTrackingTouch(SeekBar seekBar) {}
              });
          }
      }
      

      AndroidManifest.xml

      <application
          android:label="DumbCardElevation"
          android:theme="@android:style/Theme.Holo.Light" >
          <activity
              android:name=".MainActivity" >
              <intent-filter>
                  <action android:name="android.intent.action.MAIN" />
                  <category android:name="android.intent.category.LAUNCHER" />
              </intent-filter>
          </activity>
      </application>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        • 2018-02-05
        • 1970-01-01
        • 2020-06-29
        • 2018-08-11
        • 2016-10-24
        相关资源
        最近更新 更多