Canvas Path 绘制柱体

public class MainActivity extends Activity {

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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }
        
        ImageView drawingImageView;
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            
            drawingImageView = (ImageView)rootView.findViewById(R.id.drawingImageView);
            
            int width = (int) getActivity().getWindowManager().getDefaultDisplay().getWidth();
            int height = (int)getActivity().getWindowManager().getDefaultDisplay().getHeight();
            
            Bitmap bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
            
            Canvas canvas = new Canvas(bitmap);
            drawingImageView.setImageBitmap(bitmap);
            
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.GREEN);
            
            Path p = new Path();
            float offsetX = 30;
            float offsetY = 30;
            
            //正面
            p.moveTo(0.75f, 60f);
            p.lineTo(0.75f, 229.1f);
            p.lineTo(92.5f, 229.1f);
            p.lineTo(92.5f, 60f);
            p.lineTo(0.75f, 60f);
            canvas.drawPath(p, paint);

            //右侧面
            p.reset();
            p.moveTo(92.5f, 60f);
            p.lineTo(92.5f, 229.1f);
            p.lineTo(92.5f+offsetX, 229.1f-offsetY);
            p.lineTo(92.5f+offsetX, 60f-offsetY);
            p.lineTo(92.5f, 60f);
            canvas.drawPath(p, paint);
            
            //上面
            p.reset();
            p.moveTo(0.75f+offsetX, offsetY);
            p.lineTo(0.75f, 60f);
            p.lineTo(92.5f, 60f);
            p.lineTo(92.5f+offsetX, 60f-offsetY);
            p.lineTo(0.75f+offsetX, offsetY);
            canvas.drawPath(p, paint);
            
            //针对上面,绘制一个白色的菱形强化立体效果
            paint.reset();
            paint.setStyle(Style.STROKE);
            paint.setColor(Color.WHITE);
            
            p.reset();
            p.moveTo(0.75f+offsetX, offsetY);
            p.lineTo(0.75f, 60f);
            p.lineTo(92.5f, 60f);
            p.lineTo(92.5f+offsetX, 60f-offsetY);
            p.lineTo(0.75f+offsetX, offsetY);
            canvas.drawPath(p, paint);
            
            //针对右侧面,绘制一个白色菱形强化立体效果
            p.reset();
            p.moveTo(92.5f, 60f);
            p.lineTo(92.5f, 229.1f);
            p.lineTo(92.5f+offsetX, 229.1f-offsetY);
            p.lineTo(92.5f+offsetX, 60f-offsetY);
            p.lineTo(92.5f, 60f);
            canvas.drawPath(p, paint);
            
            return rootView;
        }
    }

}

 

posted @ 2014-11-22 18:13  yshy  阅读(1093)  评论(0编辑  收藏  举报