下面列出了android.support.v4.view.ViewCompat#setElevation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle args = getArguments();
if (args != null) index = args.getInt("INDEX", 0);
ViewCompat.setElevation(getView(), 10f);
ViewCompat.setElevation(toolbar, 4f);
toolbar.setTitle(Sample2Adapter.PAGE_TITLES[index]);
toolbar.inflateMenu(R.menu.fragment);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
}
/**
* Set the background drawable
* @author Melvin Lobo
*/
private void setBackgroundResource() {
//Set the background shape
if(mbElevationRequired) {
//For Lollipop and above, you can set the elevation programmatically
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mbElevationRequired = false;
ViewCompat.setElevation(this, ELEVATION);
setBackground(createBackgroundShape());
}
else
setBackground(createBackgroundShape());
}
else {
setBackground(createBackgroundShape());
}
}
@Override
public void setUpViews() {
ViewCompat.setElevation(mToolbar, getResources().getDimension(R.dimen.toolbar_elevation));
sp = getSharedPreferences("theme", MODE_PRIVATE);
color_theme = sp.getInt("color", -12627531);
tv_MingQQ= (TextView) findViewById(R.id.tv_MingQQ);
tv_MQQL= (TextView) findViewById(R.id.tv_MQQL);
tv_thanks= (TextView) findViewById(R.id.tv_thanks);
tv_MingQQ.setTextColor(color_theme);
tv_MQQL.setTextColor(color_theme);
tv_thanks.setTextColor(color_theme);
line_mingqq=findViewById(R.id.line_mingqq);
line_mqql=findViewById(R.id.line_mqql);
line_thanks=findViewById(R.id.line_thanks);
line_mingqq.setBackgroundColor(color_theme);
line_mqql.setBackgroundColor(color_theme);
line_thanks.setBackgroundColor(color_theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crop_and_rotate);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ViewCompat.setElevation(toolbar,10);
toolbar.setOnMenuItemClickListener(this);
Intent intent = getIntent();
message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
cropImageView = (CropImageView) findViewById(R.id.cropImageView);
cropImageView.setImageUriAsync(Uri.parse(message));
mFab = (FloatingActionButton) findViewById(R.id.nextStep);
mFab.setOnClickListener(this);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
Dialog result = super.onCreateDialog(savedInstanceState);
result.requestWindowFeature(Window.FEATURE_NO_TITLE);
result.setOnCancelListener(this);
if (Build.VERSION.SDK_INT >= 21)
{
// set a background with round corners
result.getWindow().setBackgroundDrawable(ContextCompat.getDrawable(getContext(), R.drawable.dmfs_colorpicker_dialog_background));
// ensure we still have the right drop shadow in place
ViewCompat.setElevation(result.getWindow().getDecorView(), 24);
}
// else: on ancient devices we'll just continue using default square corners
return result;
}
@Override
public void onAlbumSelected(Album album) {
Timber.d("onAlbumSelected: %s", album.getName());
if(detail){
super.onBackPressed();
}
detail = true;
ViewCompat.setElevation(homeContainer, 0);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
titleSwitcher.switchForward(album.getName());
// Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_ALBUM);
// if(fragment!=null){
// getSupportFragmentManager().beginTransaction()
// .remove(fragment)
// .commit();
// }
getSupportFragmentManager().beginTransaction()
.add(R.id.home_container, ProgramListFragment.newInstance(album), TAG_ALBUM)
.addToBackStack(album.getName())
.setCustomAnimations(android.R.anim.fade_out, android.R.anim.fade_out)
.commit();
}
@Override
public void onDestroy() {
super.onDestroy();
mItems.clear();
mRecyclerViewAdapter = null;
setAppBarLayoutAlpha(255);
if (mAppBarLayout != null && !isForeground()) {
mAppBarLayout.setTranslationY(0);
ViewCompat.setElevation(mAppBarLayout, 0);
}
if (mLoader != null) {
mLoader.cancel(true);
mLoader = null;
}
if (mReloader != null) {
mReloader.cancel(true);
mReloader = null;
}
if (mDialogLoader != null) {
mDialogLoader.cancel(true);
mDialogLoader = null;
}
mAdView = null;
for (RecyclerViewItem item : mItems) {
item.onDestroy();
}
}
@Override
public void onDraw(Canvas c, RecyclerView recyclerView, View view,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (isCurrentlyActive) {
Object originalElevation = view.getTag();
if (originalElevation == null) {
originalElevation = ViewCompat.getElevation(view);
float newElevation = 1f + findMaxElevation(recyclerView, view);
ViewCompat.setElevation(view, newElevation);
view.setTag(originalElevation);
}
}
super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive);
}
@Override
public void onPageSelected(int position) {
if (isOverlapEnabled) {
//Counter for loop
int loopCounter = 0;
int PAGER_LOOP_THRESHOLD = 2;
//SET THE START POINT back 2 views
if (position >= PAGER_LOOP_THRESHOLD) {
loopCounter = position - PAGER_LOOP_THRESHOLD;
}
do {
if (loopCounter < mPager.getAdapter().getCount()) {
Object object = mPager.getAdapter().instantiateItem(mPager, loopCounter);
//Elevate the Center View if it's the selected position and de-elevate the left and right fragment
if (object instanceof Fragment) {
Fragment fragment = (Fragment) object;
if (loopCounter == position) {
ViewCompat.setElevation(fragment.getView(), 8.0f);
} else {
ViewCompat.setElevation(fragment.getView(), 0.0f);
}
} else {
ViewGroup view = (ViewGroup) object;
if (loopCounter == position) {
ViewCompat.setElevation(view, 8.0f);
} else {
ViewCompat.setElevation(view, 0.0f);
}
}
}
loopCounter++;
} while (loopCounter < position + PAGER_LOOP_THRESHOLD);
}
}
@Override
public void clearView(View view) {
final Object tag = view.getTag(android.support.v7.recyclerview.R.id.item_touch_helper_previous_elevation);
if (tag != null && tag instanceof Float) {
ViewCompat.setElevation(view, (Float) tag);
}
view.setTag(android.support.v7.recyclerview.R.id.item_touch_helper_previous_elevation, null);
super.clearView(view);
}
/**
* Used internally to apply the currently set {@link SublimeThemer}.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void applyThemer() {
Log.i(TAG, "applyThemer()");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
setBackground(mThemer.getDrawerBackground());
} else {
setBackgroundDrawable(mThemer.getDrawerBackground());
}
ViewCompat.setElevation(this, mThemer.getElevation());
// propagate themer to the presenter
mPresenter.setThemer(mThemer);
}
@Override
public void onDraw(Canvas c, RecyclerView recyclerView, View view,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (isCurrentlyActive) {
Object originalElevation = view.getTag();
if (originalElevation == null) {
originalElevation = ViewCompat.getElevation(view);
float newElevation = 1f + findMaxElevation(recyclerView, view);
ViewCompat.setElevation(view, newElevation);
view.setTag(originalElevation);
}
}
super.onDraw(c, recyclerView, view, dX, dY, actionState, isCurrentlyActive);
}
public SnackbarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SnackbarLayout);
mMaxWidth = a.getDimensionPixelSize(R.styleable.SnackbarLayout_android_maxWidth, -1);
mMaxInlineActionWidth = a.getDimensionPixelSize(
R.styleable.SnackbarLayout_maxActionInlineWidth, -1);
if (a.hasValue(R.styleable.SnackbarLayout_elevation)) {
ViewCompat.setElevation(this, a.getDimensionPixelSize(
R.styleable.SnackbarLayout_elevation, 0));
}
a.recycle();
setClickable(true);
// Now inflate our content. We need to do this manually rather than using an <include>
// in the layout since older versions of the Android do not inflate includes with
// the correct Context.
LayoutInflater.from(context).inflate(com.jet.sweettips.R.layout.sweet_layout_snackbar_include, this);
ViewCompat.setAccessibilityLiveRegion(this,
ViewCompat.ACCESSIBILITY_LIVE_REGION_POLITE);
ViewCompat.setImportantForAccessibility(this,
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
// Make sure that we fit system windows and have a listener to apply any insets
ViewCompat.setFitsSystemWindows(this, true);
ViewCompat.setOnApplyWindowInsetsListener(this,
new android.support.v4.view.OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
// Copy over the bottom inset as padding so that we're displayed above the
// navigation bar
v.setPadding(v.getPaddingLeft(), v.getPaddingTop(),
v.getPaddingRight(), insets.getSystemWindowInsetBottom());
return insets;
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.observable_scroll_view_activity_toolbarcontrollistview);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
mHeaderView = findViewById(R.id.header);
ViewCompat.setElevation(mHeaderView, getResources().getDimension(R.dimen.toolbar_elevation));
mToolbarView = findViewById(R.id.toolbar);
mListView = (ObservableListView) findViewById(R.id.list);
mListView.setScrollViewCallbacks(this);
LayoutInflater inflater = LayoutInflater.from(this);
mListView.addHeaderView(inflater.inflate(R.layout.observable_scroll_view_padding, null)); // toolbar
mListView.addHeaderView(inflater.inflate(R.layout.observable_scroll_view_padding, null)); // sticky view
List<String> items = new ArrayList<String>();
for (int i = 1; i <= 100; i++) {
items.add("Item " + i);
}
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
ViewTreeObserver vto = mListView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
mListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
int count = mListView.getAdapter().getCount() - 1;
int position = count == 0 ? 1 : count > 0 ? count : 0;
mListView.smoothScrollToPosition(position);
mListView.setSelection(position);
}
});
}
public ToolbarEx(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
toolbarHeight = getResources().getDimensionPixelOffset(R.dimen.toolbar_height);
itemPadding = getResources().getDimensionPixelOffset(R.dimen.toolbar_padding);
setTitle("");
setReturnIcon(R.drawable.nav_back_grey_icon);
setTitleTextAppearance(context, R.style.NavigationTitleAppearance);
setMinimumHeight(toolbarHeight);
setBackgroundColor(Color.WHITE);
ViewCompat.setElevation(this, getResources().getDimension(R.dimen.toolbar_elevation));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
setPressed(true);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
animate().scaleX(0.98f).scaleY(0.98f).setDuration(100)
.setInterpolator(new DecelerateInterpolator());
} else {
ViewCompat.setElevation(this, 8.f);
}
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
setPressed(false);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
animate().scaleX(1.f).scaleY(1.f).setDuration(50)
.setInterpolator(new BounceInterpolator());
} else {
ViewCompat.setElevation(this, 0.f);
}
break;
}
}
return super.onTouchEvent(event);
}
@Override
public void onPageSelected(int position) {
if (isOverlapEnabled) {
//Counter for loop
int loopCounter = 0;
int PAGER_LOOP_THRESHOLD = 2;
//SET THE START POINT back 2 views
if (position >= PAGER_LOOP_THRESHOLD) {
loopCounter = position - PAGER_LOOP_THRESHOLD;
}
do {
if (loopCounter < mPager.getAdapter().getCount()) {
Object object = mPager.getAdapter().instantiateItem(mPager, loopCounter);
//Elevate the Center View if it's the selected position and de-elevate the left and right fragment
if (object instanceof Fragment) {
Fragment fragment = (Fragment) object;
if (loopCounter == position) {
ViewCompat.setElevation(fragment.getView(), 8.0f);
} else {
ViewCompat.setElevation(fragment.getView(), 0.0f);
}
} else {
ViewGroup view = (ViewGroup) object;
if (loopCounter == position) {
ViewCompat.setElevation(view, 8.0f);
} else {
ViewCompat.setElevation(view, 0.0f);
}
}
}
loopCounter++;
} while (loopCounter < position + PAGER_LOOP_THRESHOLD);
}
}
@Override
public void setUpViews() {
ViewCompat.setElevation(mToolbar, getResources().getDimension(R.dimen.toolbar_elevation));
sp = getSharedPreferences("theme", MODE_PRIVATE);
color_theme = sp.getInt("color", -12627531);
m_QQClient = AppData.getAppData().getQQClient();
m_QQClient.setCallBackHandler(m_Handler);
// m_txtCancel = (TextView)findViewById(R.id.vc_txtCancel);
// m_btnFinish = (Button)findViewById(R.id.vc_btnFinish);
m_imgVC = (ImageView)findViewById(R.id.vc_imgVC);
m_prgLogining = (CircularProgress)findViewById(R.id.vc_prgLogining);
m_edtVC = (FloatingEditText)findViewById(R.id.vc_edtVC);
m_edtVC.setNormalColor(color_theme);
m_edtVC.setHighlightedColor(color_theme);
m_prgLogining.setColor(color_theme);
// m_txtCancel.setOnClickListener(this);
// m_btnFinish.setOnClickListener(this);
// m_edtVC.addTextChangedListener(this);
byte[] bytData = m_QQClient.getVerifyCodePic();
Bitmap bmp = BitmapFactory.decodeByteArray(bytData, 0, bytData.length);
m_imgVC.setImageBitmap(bmp);
mToolbar.setNavigationIcon(R.drawable.qqicon);
mToolbar.inflateMenu(R.menu.menu_ok);
setSupportActionBar(mToolbar);
mToolbar.setOnMenuItemClickListener(onMenuItemClick);
mToolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
m_QQClient.setCallBackHandler(null);
startActivity(new Intent(VerifyCodeActivity.this, LoginActivity.class));
finish();
}
});
// Navigation Icon 要設定在 setSupoortActionBar 才有作用
// 否則會出現 back button
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (!(widthMode == 1073741824 && heightMode == 1073741824)) {
if (isInEditMode()) {
if (widthMode != Integer.MIN_VALUE) {
if (widthMode == 0) {
widthSize = 300;
}
}
if (heightMode != Integer.MIN_VALUE) {
if (heightMode == 0) {
heightSize = 300;
}
}
} else {
throw new IllegalArgumentException("DrawerLayout must be measured with MeasureSpec.EXACTLY.");
}
}
setMeasuredDimension(widthSize, heightSize);
boolean applyInsets = this.mLastInsets != null && ViewCompat.getFitsSystemWindows(this);
int layoutDirection = ViewCompat.getLayoutDirection(this);
boolean hasDrawerOnLeftEdge = false;
boolean hasDrawerOnRightEdge = false;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
if (child.getVisibility() != 8) {
MarginLayoutParams lp = (LayoutParams) child.getLayoutParams();
if (applyInsets) {
int cgrav = GravityCompat.getAbsoluteGravity(lp.gravity, layoutDirection);
if (ViewCompat.getFitsSystemWindows(child)) {
IMPL.dispatchChildInsets(child, this.mLastInsets, cgrav);
} else {
IMPL.applyMarginInsets(lp, this.mLastInsets, cgrav);
}
}
if (isContentView(child)) {
child.measure(MeasureSpec.makeMeasureSpec((widthSize - lp.leftMargin) - lp.rightMargin, 1073741824), MeasureSpec.makeMeasureSpec((heightSize - lp.topMargin) - lp.bottomMargin, 1073741824));
} else if (isDrawerView(child)) {
if (SET_DRAWER_SHADOW_FROM_ELEVATION && ViewCompat.getElevation(child) != this.mDrawerElevation) {
ViewCompat.setElevation(child, this.mDrawerElevation);
}
int childGravity = getDrawerViewAbsoluteGravity(child) & 7;
boolean isLeftEdgeDrawer = childGravity == 3;
if (!(isLeftEdgeDrawer && hasDrawerOnLeftEdge) && (isLeftEdgeDrawer || !hasDrawerOnRightEdge)) {
if (isLeftEdgeDrawer) {
hasDrawerOnLeftEdge = true;
} else {
hasDrawerOnRightEdge = true;
}
child.measure(getChildMeasureSpec(widthMeasureSpec, (this.mMinDrawerMargin + lp.leftMargin) + lp.rightMargin, lp.width), getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height));
} else {
throw new IllegalStateException("Child drawer has absolute gravity " + gravityToString(childGravity) + " but this " + TAG + " already has a " + "drawer view along that edge");
}
} else {
throw new IllegalStateException("Child " + child + " at index " + i + " does not have a valid layout_gravity - must be Gravity.LEFT, " + "Gravity.RIGHT or Gravity.NO_GRAVITY");
}
}
}
}
private FloatingActionButton createFab(Context context, int buttonDrawable, int buttonColor, int buttonPosition) {
fab = new TintFloatingActionButton(context);
int fabElevationInPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources().getDisplayMetrics());
LayoutParams fabLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
fabMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics());
if (buttonPosition == RIGHT) {
fabLayoutParams.gravity = Gravity.END | Gravity.TOP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
fabLayoutParams.setMarginEnd(fabMargin);
} else {
fabLayoutParams.rightMargin = fabMargin;
}
} else if (buttonPosition == CENTER) {
fabLayoutParams.gravity = Gravity.CENTER | Gravity.TOP;
} else {
fabLayoutParams.gravity = Gravity.START | Gravity.TOP;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
fabLayoutParams.setMarginStart(fabMargin);
} else {
fabLayoutParams.leftMargin = fabMargin;
}
}
fabLayoutParams.bottomMargin = fabMargin;
fabLayoutParams.topMargin = fabMargin;
if (buttonDrawable > 0) {
fab.setImageDrawable(ContextCompat.getDrawable(context, buttonDrawable));
}
if (buttonColor > 0) {
ViewCompat.setBackgroundTintList(fab, ContextCompat.getColorStateList(context, buttonColor));
}
ViewCompat.setElevation(fab, fabElevationInPixels);
fab.setLayoutParams(fabLayoutParams);
fab.setTag("FAB");
this.addView(fab);
return fab;
}