Skip to content

Instantly share code, notes, and snippets.

@wingoku
Last active September 25, 2015 07:21
Show Gist options
  • Save wingoku/eee1e95bea313f8a0206 to your computer and use it in GitHub Desktop.
Save wingoku/eee1e95bea313f8a0206 to your computer and use it in GitHub Desktop.
CardView shadow rendering issue
/**
* Created by wingoku on 2/4/15.
*/
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.widget.CardView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
/**
WEBVIEW - wingoku
*/
public class MyBrowser extends RelativeLayout {
private Context context;
private MyWebView webView;
ImageView crossImage;
float pivotX, pivotY;
LayoutParams imageViewParams;
DisplayMetrics metrics;
float[] webViewCoordsBackup, webViewCoordForProcessing;
boolean openingAtSpecifiedCoords = false;
boolean slideInAnimation = false;
int animationDuration;
CardView mCardView;
LayoutParams mCardViewParams;
public MyBrowser(Context context) {
super(context);
this.context = context;
init();
}
public MyBrowser(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public MyBrowser(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
private void init() {
setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
setBackgroundColor(Color.WHITE);
setPadding(0,0,0,0);
metrics = context.getResources().getDisplayMetrics();
webView = new MyWebView(context, 1994);
webView.setMyWebViewEventListener(this);
mCardView = new CardView(context);
mCardView.setId(1995);
//mCardView.setCardBackgroundColor(Color.BLUE);
//mCardView.setPreventCornerOverlap(true);
mCardView.setCardElevation(dpToPixels(10));
mCardView.setMaxCardElevation(dpToPixels(10));
mCardView.setUseCompatPadding(true);
mCardView.addView(webView.getWebviewInstance());
//mCardView.setRadius(1f);
mCardViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mCardViewParams.topMargin = dpToPixels(28);
mCardViewParams.leftMargin = dpToPixels(28);
mCardViewParams.rightMargin = dpToPixels(28);
mCardViewParams.bottomMargin = dpToPixels(28);
mCardView.setLayoutParams(mCardViewParams);
//addView(mCardView,mCardViewParams); // adding CardView to MyBrowser & then adding MyBrowser to xml inflated RelativeLayout doesn't render shadows. Adding cardView directly to xml inflated RelativeLayout render shadows
}
public void loadBrowser(String url, float pivotX, float pivotY) {
this.setLayerType(LAYER_TYPE_SOFTWARE, null);
this.pivotX = pivotX;
this.pivotY = pivotY;
webView.getWebviewInstance().loadUrl(url);
Animation anim;
if(slideInAnimation)
{
animationDuration = 500;
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
mCardViewParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
mCardViewParams.rightMargin = -1;
anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
}
else {
mCardViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mCardViewParams.bottomMargin = -1;
anim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0);
}
}
}
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(context, "Land", Toast.LENGTH_SHORT).show();
mCardViewParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
else {
Toast.makeText(context, "PORT", Toast.LENGTH_SHORT).show();
mCardViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
}
private int dpToPixels(int dp)
{
return (int)getResources().getDisplayMetrics().density * dp;
}
public CardView getCard(){
return mCardView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment