Android5.x新特性之ViewOutlineProvider轮廓、阴影---setClipToOutline裁剪

——————————————————效果1————————————————————

Android5.x新特性之ViewOutlineProvider轮廓、阴影---setClipToOutline裁剪_第1张图片

activity_main.xml主界面布局文件如下




    




MainActivity主界面代码实现

package com.imooc.material.shadows.drag;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;


public class MainActivity extends FragmentActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            ElevationDragFragment fragment = new ElevationDragFragment();
            transaction.replace(R.id.sample_content_fragment, fragment);
            transaction.commit();
        }
    }

  
}

ztranslation.xml  fragment布局如下




    

    

        



ElevationDragFragment

package com.imooc.material.shadows.drag;

import android.graphics.Outline;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;

public class ElevationDragFragment extends Fragment {

	private ViewOutlineProvider mOutlineProviderCircle;

	private float mElevation = 0;

	private int mElevationStep;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mOutlineProviderCircle = new CircleOutlineProvider();

		mElevationStep = getResources().getDimensionPixelSize(
				R.dimen.elevation_step);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.ztranslation, container,
				false);
		final View floatingShape = rootView.findViewById(R.id.circle);

		floatingShape.setOutlineProvider(mOutlineProviderCircle);
		floatingShape.setClipToOutline(true);

		rootView.findViewById(R.id.raise_bt).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						mElevation += mElevationStep;
						floatingShape.setElevation(mElevation);

					}
				});

		rootView.findViewById(R.id.lower_bt).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						mElevation -= mElevationStep;
						if (mElevation < 0) {
							mElevation = 0;
						}

						floatingShape.setElevation(mElevation);

					}
				});
		return rootView;
	}

	private class CircleOutlineProvider extends ViewOutlineProvider {
		@Override
		public void getOutline(View view, Outline outline) {
			outline.setOval(0, 0, view.getWidth(), view.getHeight());

		}
	}

}


——————————————————效果2————————————————————

Android5.x新特性之ViewOutlineProvider轮廓、阴影---setClipToOutline裁剪_第2张图片

activity_main.xml 主界面布局问下如下




    



主界面代码如下MainActivity

package com.imooc.material.shadows.drag;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;


public class MainActivity extends FragmentActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            ElevationDragFragment fragment = new ElevationDragFragment();
            transaction.replace(R.id.sample_content_fragment, fragment);
            transaction.commit();
        }
    }

  
}

ztranslation.xml




    

    

        

ElevationDragFragment

package com.imooc.material.shadows.drag;

import android.graphics.Outline;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.widget.Button;

public class ElevationDragFragment extends Fragment {

	private ViewOutlineProvider mOutlineProviderCircle;

	private float mElevation = 0;

	private int mElevationStep;

	Button button;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mOutlineProviderCircle = new CircleOutlineProvider();

		mElevationStep = getResources().getDimensionPixelSize(
				R.dimen.elevation_step);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.ztranslation, container,
				false);
		button = (Button) rootView.findViewById(R.id.button);
		final View floatingShape = rootView.findViewById(R.id.circle);

		floatingShape.setOutlineProvider(mOutlineProviderCircle);
		floatingShape.setClipToOutline(true);

		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//如果处于裁剪状态,恢复即可
				if (floatingShape.getClipToOutline()) {
					floatingShape.setClipToOutline(false);
					
				}else {
					floatingShape.setClipToOutline(true);
				}
			}
		});
		
		rootView.findViewById(R.id.raise_bt).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						mElevation += mElevationStep;
						floatingShape.setElevation(mElevation);

					}
				});

		rootView.findViewById(R.id.lower_bt).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						mElevation -= mElevationStep;
						if (mElevation < 0) {
							mElevation = 0;
						}

						floatingShape.setElevation(mElevation);

					}
				});
		return rootView;
	}

	private class CircleOutlineProvider extends ViewOutlineProvider {
		@Override
		public void getOutline(View view, Outline outline) {
			int margin = Math.min(view.getWidth(), view.getHeight())/10;
			//view是match_parent的
			outline.setRoundRect(margin, margin, view.getWidth() - margin, view.getHeight() , margin/2);
		}
	}

}




你可能感兴趣的:(Android基础)