1ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu/* 2ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * Copyright (C) 2017 The Android Open Source Project 3ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * 4ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * Licensed under the Apache License, Version 2.0 (the "License"); 5ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * you may not use this file except in compliance with the License. 6ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * You may obtain a copy of the License at 7ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * 8ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * http://www.apache.org/licenses/LICENSE-2.0 9ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * 10ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * Unless required by applicable law or agreed to in writing, software 11ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * distributed under the License is distributed on an "AS IS" BASIS, 12ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * See the License for the specific language governing permissions and 14ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * limitations under the License. 15ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu */ 16ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu 17ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liupackage com.example.android.support.animation; 18ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu 19ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liuimport android.content.Context; 20ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liuimport android.graphics.Canvas; 21ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liuimport android.graphics.Paint; 22ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liuimport android.util.AttributeSet; 23ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liuimport android.view.View; 24ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu 25ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu/** 26ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * The view that draws the spring as it reacts (i.e. expands/compresses) to the user touch. 27ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu */ 28ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liupublic class SpringView extends View { 29ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu final Paint mPaint = new Paint(); 30ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu private float mLastHeight = 175; 31ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu 32ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu public SpringView(Context context, AttributeSet attrs) { 33ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu super(context, attrs); 34ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu setWillNotDraw(false); 35ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu mPaint.setColor(context.getResources().getColor(R.color.springColor)); 36ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu mPaint.setStrokeWidth(10); 37ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu } 38ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu 39ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu /** 40ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * Sets the other end of the spring. 41ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * 42ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu * @param height height of the mass, which is used to derive how to draw the spring 43ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu */ 44ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu public void setMassHeight(float height) { 45ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu mLastHeight = height; 46ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu invalidate(); 47ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu } 48ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu 49ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu @Override 50ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu public void onDraw(Canvas canvas) { 51ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu // Draws the spring 52ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu // 30px long, 15 sections 53ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu int num = 20; 54ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu float sectionLen = 150; // px 55ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu final float x = canvas.getWidth() / 2; 56ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu float y = 0; 57ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu float sectionHeight = mLastHeight / num; 58ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu float sectionWidth = (float) Math.sqrt(sectionLen * sectionLen 59ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu - sectionHeight * sectionHeight); 60ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu canvas.drawLine(x, 0, x + sectionWidth / 2, sectionHeight / 2, mPaint); 61ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu float lastX = x + sectionWidth / 2; 62ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu float lastY = sectionHeight / 2; 63ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu for (int i = 1; i < num; i++) { 64ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu canvas.drawLine(lastX, lastY, 2 * x - lastX, lastY + sectionHeight, mPaint); 65ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu lastX = 2 * x - lastX; 66ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu lastY = lastY + sectionHeight; 67ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu } 68ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu canvas.drawLine(lastX, lastY, x, mLastHeight, mPaint); 69ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu } 70ac7cef74baee101ea2c0e22a2dea5b68632d9dc1Doris Liu} 71