Search in sources :

Example 1 with ContextPrice

use of com.shiming.andrioddesignpattern.strategy_model.ContextPrice in project AndriodDesignPattern by Shimingli.

the class StrategyModelFragment method onActivityCreated.

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FivePriceStrategy fivePriceStrategy = new FivePriceStrategy();
    ContextPrice contextPrice = new ContextPrice(fivePriceStrategy);
    int i = contextPrice.setNewPrice(10);
    SevenPriceStrategy sevenPriceStrategy = new SevenPriceStrategy();
    contextPrice.setNewStrategy(sevenPriceStrategy);
    int i1 = contextPrice.setNewPrice(20);
    mTextView = (TextView) getView().findViewById(R.id.tvdes);
    final TextView viewById1 = (TextView) getView().findViewById(R.id.tv_text);
    mTextView.setText("第一种策略模式实现的价格:\n原价是10 减半后" + i + "\n" + "第二种策略模式实现的价格\n原价是20 7折后" + i1);
    // 时间差值器,也是策略模式
    final Button viewById = (Button) getView().findViewById(R.id.btn);
    viewById.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(viewById1, View.ALPHA, 0, 1);
            // 这里就相当于策略模式实现的
            // 加速
            objectAnimator.setInterpolator(new AccelerateInterpolator());
            // huilai
            objectAnimator.setInterpolator(new OvershootInterpolator());
            objectAnimator.start();
        }
    });
}
Also used : ContextPrice(com.shiming.andrioddesignpattern.strategy_model.ContextPrice) AccelerateInterpolator(android.view.animation.AccelerateInterpolator) OvershootInterpolator(android.view.animation.OvershootInterpolator) Button(android.widget.Button) ObjectAnimator(android.animation.ObjectAnimator) SevenPriceStrategy(com.shiming.andrioddesignpattern.strategy_model.SevenPriceStrategy) TextView(android.widget.TextView) TextView(android.widget.TextView) View(android.view.View) FivePriceStrategy(com.shiming.andrioddesignpattern.strategy_model.FivePriceStrategy)

Aggregations

ObjectAnimator (android.animation.ObjectAnimator)1 View (android.view.View)1 AccelerateInterpolator (android.view.animation.AccelerateInterpolator)1 OvershootInterpolator (android.view.animation.OvershootInterpolator)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 ContextPrice (com.shiming.andrioddesignpattern.strategy_model.ContextPrice)1 FivePriceStrategy (com.shiming.andrioddesignpattern.strategy_model.FivePriceStrategy)1 SevenPriceStrategy (com.shiming.andrioddesignpattern.strategy_model.SevenPriceStrategy)1